System.InvalidOperationException: '无法分配没有元素的本机控件;
System.InvalidOperationException: 'Cannot assign a native control without an Element;
我正在做一个项目"CoManga",我想在其中添加广告。在 UWP 上实施广告似乎很简单,例如 Android 和 iOS。但是,我现在卡住了。
无论如何,我跟着this tutorial by James Montemagno添加了所有内容。我什至看到了测试广告,这很棒。但是,当我尝试离开该页面(当我按 "BACK Button" 时)并转到上一页时,出现错误。
这是错误:
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.'.
它被抛出在 line number 50,我设置 SetNativeControl(adView);
的地方。我现在已经把它注释掉了,但是一旦我取消注释,我就会看到这个错误。
谁能帮我解决这个问题。
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.
原因是xamarin元素已经释放但是SetNativeControl
再次调用导致页面返回时原生控件找不到匹配的xamarin元素。所以你可以设置一个标志(isRegist
)来记录注册的广告。
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
};
}
}
我正在做一个项目"CoManga",我想在其中添加广告。在 UWP 上实施广告似乎很简单,例如 Android 和 iOS。但是,我现在卡住了。
无论如何,我跟着this tutorial by James Montemagno添加了所有内容。我什至看到了测试广告,这很棒。但是,当我尝试离开该页面(当我按 "BACK Button" 时)并转到上一页时,出现错误。
这是错误:
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.'.
它被抛出在 line number 50,我设置 SetNativeControl(adView);
的地方。我现在已经把它注释掉了,但是一旦我取消注释,我就会看到这个错误。
谁能帮我解决这个问题。
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.
原因是xamarin元素已经释放但是SetNativeControl
再次调用导致页面返回时原生控件找不到匹配的xamarin元素。所以你可以设置一个标志(isRegist
)来记录注册的广告。
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
};
}
}