最初的问题来自于关于折线的个人项目Xamarin.Forms.Map https://github.com/Emixam23/XamarinByEmixam23/tree/master/Detailed%20Part/Controls/Map/MapPolylineProject其中初始化是通过 XAML 部分的绑定来实现的。
让我通过一个例子来清楚地说明:
我有一个对象自定义地图.cs继承自Xamarin.Forms.Map
(该文件位于PCL部分->CustomControl/CustomMap.cs)
public class CustomMap : Map, INotifyPropertyChanged
{
public static readonly BindableProperty PolylineAddressPointsProperty =
BindableProperty.Create(nameof(PolylineAddressPoints), typeof(List<string>), typeof(CustomMap), null);
public List<string> PolylineAddressPoints
{
get { return (List<string>)GetValue(PolylineAddressPointsProperty); }
set
{
SetValue(PolylineAddressPointsProperty, value);
this.GeneratePolylineCoordinatesInner();
}
}
// ...
}
正如您所看到的,我有一个带有评估器的可绑定属性,并且 XAML 似乎没有使用此评估器。
So the MainPge.xaml调用该控件的页面部分如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:control="clr-namespace:MapPolylineProject.CustomControl;assembly=MapPolylineProject"
x:Class="MapPolylineProject.Page.MainPage">
<ContentPage.Content>
<control:CustomMap x:Name="MapTest" PolylineAddressPoints="{Binding AddressPointList}"
VerticalOptions="Fill" HorizontalOptions="Fill"/>
</ContentPage.Content>
</ContentPage>
The MainPge.xaml.cs part:
public partial class MainPage : ContentPage
{
public List<string> AddressPointList { get; set; }
public MainPage()
{
base.BindingContext = this;
AddressPointList = new List<string>()
{
"72230 Ruaudin, France",
"72100 Le Mans, France",
"77500 Chelles, France"
};
InitializeComponent();
//MapTest.PolylineAddressPoints = AddressPointList;
}
}
所以,如果我编辑,一切都很好PolylineAddressPoints
从对象实例(如果评论的部分没有被评论..),但是如果我从 XAML 初始化值(从InitializeComponent();
),它不起作用,SetValue
, 在里面Set {}
, 的CustomMap.PolylineAddressPoints
, 不叫..
然后我在网上搜索了相关内容并得到了一些关于依赖属性?或类似的东西。所以我尝试了一些解决方案,但是,从WPF,所以一些方法,例如DependencyProperty.Register();
。所以是的,我找不到解决问题的方法..
我也想过一些事情,如果DependencyProperty.Register();
将存在于Xamarin.Forms,那么这意味着我必须对每个值都这样做?因为,如果每个值都必须由XAML 绑定逻辑,这是行不通的,我必须注册每个值,不是吗?
如果我不清楚,我很抱歉,但我对这个问题很迷茫。请随时询问更多详细信息,提前致谢!
最后,最初的问题是我试图从 XAML 设置对象/控件的值。通过绑定执行此操作不起作用,似乎它被忽略了。但是,如果我执行以下操作,它确实有效:
MapTest.PolylineAddressPoints = AddressPointList;