我刚刚将我的Xamarin Forms项目迁移到了.NET Standard 2.0,并且正在解决一些奇怪的问题。在以下情况下,我在ListDictionaryInternal上收到一个Null引用异常,没有异常断点,没有堆栈跟踪,也没有其他可用信息。从我的视图模型来看,这种情况是在自定义按钮控件上设置可绑定文本属性。我已经调试了整个文本设置过程,之后发生了异常。我完全被这个困扰。而且要明确的是,此代码在.NET Standard 2.0迁移之前已经起作用。先感谢您!
更新:
因此,Forms iOS也开始发生此异常,但是现在当System.Exception断点命中时,我能够获得堆栈跟踪。同样,这与.NET Standard 2.0不相关,因为在针对PCL 4.5项目(准确地说是Profile 111)时也会发生这种情况。
我可以在OnSizeAllocated重写方法后面的代码中设置一个断点,然后查看视图是否接受了绑定文本并正确地进行了布局。因此,问题不是绑定,而是与视图布局有关。
另外,为澄清起见,仅当文本设置为具有可绑定属性时,才会发生此异常。如果在xaml中设置了文本,则不会发生异常。
ViewModel ...
public class SimpleSearchViewModel : BaseViewModel
{
enum SearchCatagories
{
All,
SubjectId,
PublicationNumber
}
public override void OnStart()
{
UpdateTypeButton("ALL");
}
private void UpdateTypeButton(string item)
{
SearchTypeButtonText = item;
}
private string _searchTypeButtonText;
public string SearchTypeButtonText
{
get
{
return _searchTypeButtonText;
}
private set
{
_searchTypeButtonText = value;
OnPropertyChanged(nameof(SearchTypeButtonText));
}
}
}
风景...
<core:BasePage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="using:App.ViewModels"
xmlns:customcontrols="clr-namespace:App;assembly=App"
xmlns:core="clr-namespace:App.Core;assembly=App.Core"
x:Class="Pages.SimpleSearchPage"
Title="Simple Search">
<ContentPage.BindingContext>
<viewmodels:SimpleSearchViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout Padding=" 10, 10, 10, 10"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Orientation="Horizontal" >
<customcontrols:SVGImageButton x:Name="TypeSelectionButton"
HorizontalOptions="Start"
VerticalOptions="Center"
ButtonPressedCommand="{Binding TypeButtonClickedCommand}"
SVGImageName="SVGImages.ic_triangle_down.svg"
CommandParameter="{x:Reference TypeSelectionButton}"
ButtonText="{Binding SearchTypeButtonText}"
ButtonBackgroundColor="{Binding ButtonBackgroundColor}"/>
</StackLayout>
</ContentPage.Content>
</core:BasePage>
SVGImageButton ...
[XamlCompilation(XamlCompilationOptions.Compile)]
public class SVGImageButton : ContentView
{
private readonly Button _button;
private readonly SvgCachedImage _svgImage;
public static BindableProperty ButtonTextProperty = BindableProperty.Create(nameof(ButtonText), typeof(string), typeof(SVGImageButton), string.Empty, BindingMode.OneWay, propertyChanged: (bindable, oldValue, newValue) =>
{
if (newValue == null) return;
var control = (SVGImageButton)bindable;
control.ButtonText = newValue.ToString();
});
public string ButtonText
{
get
{
return _button.Text;
}
set
{
_button.Text = value;
}
}
public string SVGImageName { get; set; }
protected override void OnParentSet()
{
base.OnParentSet();
_button.Text = ButtonText;
_svgImage.Source = SvgImageSource.FromResource(SVGImageName);
}
public SVGImageButton()
{
var content = new RelativeLayout();
_button = new Button { BackgroundColor = Color.Gray, TextColor = Color.Black };
_svgImage = new SvgCachedImage { IsEnabled = false };
content.Children.Add(_button,
Constraint.RelativeToParent((parent) =>
{
return parent.X;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Y;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Width;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height;
}));
content.Children.Add(_svgImage,
Constraint.RelativeToParent((parent) =>
{
return parent.Width - (parent.Height / 2) - (parent.Height / 4);
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height - (parent.Height / 2) - (parent.Height / 4);
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height / 2;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height / 2;
}));
Content = content;
}
}
}
因此,这个问题最终源于我在OnMeasure回调中如何处理控件的大小。在这里可以看到正确的问题和答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句