System.InvalidOperationException: 'Cannot assign a native control without an Element;Setting Objects to Null/Nothing after use in .NET“The Controls collection cannot be modified because the control contains code blocks”Alternating Row Colors in Xamarin Forms ListView IOSMediaTimelineController throwing COMException on FrameworkElement.OnMeasureOverride?UWP app WebView.NavigateToString() Iframe Javascript Access DeniedXamarin Forms - Custom Renderer Can't Get Native Control (UWP)Xamarin.Forms UWP Startup Issues regarding NavigationPage as App MainPageWhat's the proper way of setting up MvvmCross 6.0 Xamarin.Forms UWP application code?How to set UWP language on runtimeConnecting Xamarin Form tabbed pages to XAML

What should I call bands of armed men in the Middle Ages?

Understanding this peak detector circuit

Why isn’t SHA-3 in wider use?

How can Radagast come across Gandalf and Thorin's company?

How to take the beginning and end parts of a list with simpler syntax?

Solution to German Tank Problem

Not going forward with internship interview process

Super Duper Vdd stiffening required on 555 timer, what is the best way?

Can the ground attached to neutral fool a receptacle tester?

How to write hyperlinks to local files in GeoJSON properties?

Bitcoin successfully deducted on sender wallet but did not reach receiver wallet

Plotting octahedron inside the sphere and sphere inside the cube

If "more guns less crime", how do gun advocates explain that the EU has less crime than the US?

Breadcrumb history decision

Simplification of numbers

How can I decide if my homebrew item should require attunement?

How can this older-style irrigation tee be replaced?

When were the tantalum capacitors first used in computing?

How many people would you need to pull a whale over cobblestone streets?

On math looking obvious in retrospect

A Non Math Puzzle. What is the middle number?

Generate Brainfuck for the numbers 1–255

How to divide item stack in MC PE?

When does Tiana, Ship's Caretaker check card type?



System.InvalidOperationException: 'Cannot assign a native control without an Element;


Setting Objects to Null/Nothing after use in .NET“The Controls collection cannot be modified because the control contains code blocks”Alternating Row Colors in Xamarin Forms ListView IOSMediaTimelineController throwing COMException on FrameworkElement.OnMeasureOverride?UWP app WebView.NavigateToString() Iframe Javascript Access DeniedXamarin Forms - Custom Renderer Can't Get Native Control (UWP)Xamarin.Forms UWP Startup Issues regarding NavigationPage as App MainPageWhat's the proper way of setting up MvvmCross 6.0 Xamarin.Forms UWP application code?How to set UWP language on runtimeConnecting Xamarin Form tabbed pages to XAML






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am working on a project "CoManga" and I wanted to add advertisements in it. Implementing ads on UWP seemed straight forward, like Android and iOS. However, I'm stuck now.



Anyways, I followed this tutorial by James Montemagno and added everything. I even see the test advertisements, which is great. However, when I try to move away from that page (when I press "BACK Button") and go to previous page, I get an error.



This is the error :



Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.'.



It is thrown at line number 50, where I set the SetNativeControl(adView);. I've commented it out right now, but as soon as I un-comment it, I see this error.



Can someone help me out here with this.










share|improve this question
























  • Does the following answer work?

    – Nico Zhu - MSFT
    Mar 28 at 15:54











  • Sorry, I haven't been able to put this one around. So couldn't check. Will try to check it this evening and will mark it as solved if it works.

    – Xonshiz
    Mar 29 at 8:50

















0















I am working on a project "CoManga" and I wanted to add advertisements in it. Implementing ads on UWP seemed straight forward, like Android and iOS. However, I'm stuck now.



Anyways, I followed this tutorial by James Montemagno and added everything. I even see the test advertisements, which is great. However, when I try to move away from that page (when I press "BACK Button") and go to previous page, I get an error.



This is the error :



Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.'.



It is thrown at line number 50, where I set the SetNativeControl(adView);. I've commented it out right now, but as soon as I un-comment it, I see this error.



Can someone help me out here with this.










share|improve this question
























  • Does the following answer work?

    – Nico Zhu - MSFT
    Mar 28 at 15:54











  • Sorry, I haven't been able to put this one around. So couldn't check. Will try to check it this evening and will mark it as solved if it works.

    – Xonshiz
    Mar 29 at 8:50













0












0








0








I am working on a project "CoManga" and I wanted to add advertisements in it. Implementing ads on UWP seemed straight forward, like Android and iOS. However, I'm stuck now.



Anyways, I followed this tutorial by James Montemagno and added everything. I even see the test advertisements, which is great. However, when I try to move away from that page (when I press "BACK Button") and go to previous page, I get an error.



This is the error :



Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.'.



It is thrown at line number 50, where I set the SetNativeControl(adView);. I've commented it out right now, but as soon as I un-comment it, I see this error.



Can someone help me out here with this.










share|improve this question














I am working on a project "CoManga" and I wanted to add advertisements in it. Implementing ads on UWP seemed straight forward, like Android and iOS. However, I'm stuck now.



Anyways, I followed this tutorial by James Montemagno and added everything. I even see the test advertisements, which is great. However, when I try to move away from that page (when I press "BACK Button") and go to previous page, I get an error.



This is the error :



Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.'.



It is thrown at line number 50, where I set the SetNativeControl(adView);. I've commented it out right now, but as soon as I un-comment it, I see this error.



Can someone help me out here with this.







c# xamarin xamarin.forms uwp custom-renderer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 8:57









XonshizXonshiz

3312 gold badges4 silver badges29 bronze badges




3312 gold badges4 silver badges29 bronze badges















  • Does the following answer work?

    – Nico Zhu - MSFT
    Mar 28 at 15:54











  • Sorry, I haven't been able to put this one around. So couldn't check. Will try to check it this evening and will mark it as solved if it works.

    – Xonshiz
    Mar 29 at 8:50

















  • Does the following answer work?

    – Nico Zhu - MSFT
    Mar 28 at 15:54











  • Sorry, I haven't been able to put this one around. So couldn't check. Will try to check it this evening and will mark it as solved if it works.

    – Xonshiz
    Mar 29 at 8:50
















Does the following answer work?

– Nico Zhu - MSFT
Mar 28 at 15:54





Does the following answer work?

– Nico Zhu - MSFT
Mar 28 at 15:54













Sorry, I haven't been able to put this one around. So couldn't check. Will try to check it this evening and will mark it as solved if it works.

– Xonshiz
Mar 29 at 8:50





Sorry, I haven't been able to put this one around. So couldn't check. Will try to check it this evening and will mark it as solved if it works.

– Xonshiz
Mar 29 at 8:50












1 Answer
1






active

oldest

votes


















2















Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.




The reason is that xamarin Element has released but SetNativeControl invoked again cause the native control can't find the matched xamarin Element when page going back. So you could set a flag (isRegist) to record the registed ad.



public class AdViewRenderer : ViewRenderer<AdControlView, AdControl>

string bannerId = "test";
AdControl adView;
string applicationID = "3f83fe91-d6be-434d-a0ae-7351c5a997f1";
bool isRegist = false;

protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)

base.OnElementChanged(e);

if (Control == null && isRegist != true)

CreateNativeAdControl();
SetNativeControl(adView);
isRegist = true;


private void CreateNativeAdControl()

if (adView != null)
return;

var width = 300;
var height = 50;
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")

width = 728;
height = 90;

// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new AdControl

ApplicationId = applicationID,
AdUnitId = bannerId,
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom,
Height = height,
Width = width
;







share|improve this answer

























  • So, this worked like a charm. Thanks a ton Nico...

    – Xonshiz
    Mar 29 at 19:40










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55373197%2fsystem-invalidoperationexception-cannot-assign-a-native-control-without-an-ele%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2















Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.




The reason is that xamarin Element has released but SetNativeControl invoked again cause the native control can't find the matched xamarin Element when page going back. So you could set a flag (isRegist) to record the registed ad.



public class AdViewRenderer : ViewRenderer<AdControlView, AdControl>

string bannerId = "test";
AdControl adView;
string applicationID = "3f83fe91-d6be-434d-a0ae-7351c5a997f1";
bool isRegist = false;

protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)

base.OnElementChanged(e);

if (Control == null && isRegist != true)

CreateNativeAdControl();
SetNativeControl(adView);
isRegist = true;


private void CreateNativeAdControl()

if (adView != null)
return;

var width = 300;
var height = 50;
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")

width = 728;
height = 90;

// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new AdControl

ApplicationId = applicationID,
AdUnitId = bannerId,
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom,
Height = height,
Width = width
;







share|improve this answer

























  • So, this worked like a charm. Thanks a ton Nico...

    – Xonshiz
    Mar 29 at 19:40















2















Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.




The reason is that xamarin Element has released but SetNativeControl invoked again cause the native control can't find the matched xamarin Element when page going back. So you could set a flag (isRegist) to record the registed ad.



public class AdViewRenderer : ViewRenderer<AdControlView, AdControl>

string bannerId = "test";
AdControl adView;
string applicationID = "3f83fe91-d6be-434d-a0ae-7351c5a997f1";
bool isRegist = false;

protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)

base.OnElementChanged(e);

if (Control == null && isRegist != true)

CreateNativeAdControl();
SetNativeControl(adView);
isRegist = true;


private void CreateNativeAdControl()

if (adView != null)
return;

var width = 300;
var height = 50;
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")

width = 728;
height = 90;

// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new AdControl

ApplicationId = applicationID,
AdUnitId = bannerId,
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom,
Height = height,
Width = width
;







share|improve this answer

























  • So, this worked like a charm. Thanks a ton Nico...

    – Xonshiz
    Mar 29 at 19:40













2












2








2








Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.




The reason is that xamarin Element has released but SetNativeControl invoked again cause the native control can't find the matched xamarin Element when page going back. So you could set a flag (isRegist) to record the registed ad.



public class AdViewRenderer : ViewRenderer<AdControlView, AdControl>

string bannerId = "test";
AdControl adView;
string applicationID = "3f83fe91-d6be-434d-a0ae-7351c5a997f1";
bool isRegist = false;

protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)

base.OnElementChanged(e);

if (Control == null && isRegist != true)

CreateNativeAdControl();
SetNativeControl(adView);
isRegist = true;


private void CreateNativeAdControl()

if (adView != null)
return;

var width = 300;
var height = 50;
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")

width = 728;
height = 90;

// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new AdControl

ApplicationId = applicationID,
AdUnitId = bannerId,
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom,
Height = height,
Width = width
;







share|improve this answer














Setting up AdControlView in UWP throws System.InvalidOperationException: 'Cannot assign a native control without an Element; Renderer unbound and/or disposed. Please consult Xamarin.Forms renderers for reference implementation of OnElementChanged.




The reason is that xamarin Element has released but SetNativeControl invoked again cause the native control can't find the matched xamarin Element when page going back. So you could set a flag (isRegist) to record the registed ad.



public class AdViewRenderer : ViewRenderer<AdControlView, AdControl>

string bannerId = "test";
AdControl adView;
string applicationID = "3f83fe91-d6be-434d-a0ae-7351c5a997f1";
bool isRegist = false;

protected override void OnElementChanged(ElementChangedEventArgs<AdControlView> e)

base.OnElementChanged(e);

if (Control == null && isRegist != true)

CreateNativeAdControl();
SetNativeControl(adView);
isRegist = true;


private void CreateNativeAdControl()

if (adView != null)
return;

var width = 300;
var height = 50;
if (AnalyticsInfo.VersionInfo.DeviceFamily == "Windows.Desktop")

width = 728;
height = 90;

// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new AdControl

ApplicationId = applicationID,
AdUnitId = bannerId,
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Center,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom,
Height = height,
Width = width
;








share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 28 at 3:28









Nico Zhu - MSFTNico Zhu - MSFT

12.9k1 gold badge6 silver badges24 bronze badges




12.9k1 gold badge6 silver badges24 bronze badges















  • So, this worked like a charm. Thanks a ton Nico...

    – Xonshiz
    Mar 29 at 19:40

















  • So, this worked like a charm. Thanks a ton Nico...

    – Xonshiz
    Mar 29 at 19:40
















So, this worked like a charm. Thanks a ton Nico...

– Xonshiz
Mar 29 at 19:40





So, this worked like a charm. Thanks a ton Nico...

– Xonshiz
Mar 29 at 19:40








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55373197%2fsystem-invalidoperationexception-cannot-assign-a-native-control-without-an-ele%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript