PancakeView Lebel 文本绑定不起作用
PancakeView Lebel Text Binding not working
我在我的 xamarin 表单项目中使用 PancakeView Nuget,这是我的代码:
<yummy:PancakeView Margin="0,32,0,0" BackgroundColor="#E96C20" Padding="20,0"
HorizontalOptions="FillAndExpand" HeightRequest="150" CornerRadius="20">
<Label Text="{Binding Points}" HorizontalTextAlignment="Center" FontSize="Large"
HorizontalOptions="Center" VerticalOptions="Center" TextColor="#000000" />
<yummy:PancakeView.Shadow>
<yummy:DropShadow x:Name="shadow" Color="Gray" Offset="30, 30"/>
</yummy:PancakeView.Shadow>
</yummy:PancakeView>
如您所见,我正在尝试从名为 points 的视图模型中绑定一个字符串,下面是它的 C# 代码:
public class PointsPageViewModel : ContentPage
{
public string Points = "0";
}
但是,即使我确实在 xml 文件的开头定义了绑定上下文,它似乎也不起作用:
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
xmlns:vm="clr-namespace:MyProject.ViewModels"
mc:Ignorable="d"
x:Class="MyProject.Views.PointsPage" Title="POINTS">
<ContentPage.BindingContext>
<vm:PointsPageViewModel/>
</ContentPage.BindingContext>
所以我不知道为什么会这样,绑定在 pancakeview 之外工作正常,所以我不明白为什么它不能在其中工作。
如果有人以前遇到过这个问题并且知道如何解决,请告诉我!
您 xml 类 需要看起来像下面的代码才能正确反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
StringReader sReader = new StringReader(xml);
XmlReader xReader = XmlReader.Create(sReader);
XmlSerializer serializer = new XmlSerializer(typeof(ContentPage));
ContentPage contentPage = (ContentPage)serializer.Deserialize(xReader);
}
}
[XmlRoot(Namespace = "http://xamarin.com/schemas/2014/forms")]
public class ContentPage
{
[XmlElement(ElementName = "ContentPage.BindingContext", Namespace = "http://xamarin.com/schemas/2014/forms")]
public BindingContext BindingContext { get; set; }
}
public class BindingContext
{
[XmlElement(Namespace = "clr-namespace:MyProject.ViewModels")]
public string PointsPageViewModel { get; set; }
}
}
我在我的 xamarin 表单项目中使用 PancakeView Nuget,这是我的代码:
<yummy:PancakeView Margin="0,32,0,0" BackgroundColor="#E96C20" Padding="20,0"
HorizontalOptions="FillAndExpand" HeightRequest="150" CornerRadius="20">
<Label Text="{Binding Points}" HorizontalTextAlignment="Center" FontSize="Large"
HorizontalOptions="Center" VerticalOptions="Center" TextColor="#000000" />
<yummy:PancakeView.Shadow>
<yummy:DropShadow x:Name="shadow" Color="Gray" Offset="30, 30"/>
</yummy:PancakeView.Shadow>
</yummy:PancakeView>
如您所见,我正在尝试从名为 points 的视图模型中绑定一个字符串,下面是它的 C# 代码:
public class PointsPageViewModel : ContentPage
{
public string Points = "0";
}
但是,即使我确实在 xml 文件的开头定义了绑定上下文,它似乎也不起作用:
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:yummy="clr-namespace:Xamarin.Forms.PancakeView;assembly=Xamarin.Forms.PancakeView"
xmlns:vm="clr-namespace:MyProject.ViewModels"
mc:Ignorable="d"
x:Class="MyProject.Views.PointsPage" Title="POINTS">
<ContentPage.BindingContext>
<vm:PointsPageViewModel/>
</ContentPage.BindingContext>
所以我不知道为什么会这样,绑定在 pancakeview 之外工作正常,所以我不明白为什么它不能在其中工作。 如果有人以前遇到过这个问题并且知道如何解决,请告诉我!
您 xml 类 需要看起来像下面的代码才能正确反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
StringReader sReader = new StringReader(xml);
XmlReader xReader = XmlReader.Create(sReader);
XmlSerializer serializer = new XmlSerializer(typeof(ContentPage));
ContentPage contentPage = (ContentPage)serializer.Deserialize(xReader);
}
}
[XmlRoot(Namespace = "http://xamarin.com/schemas/2014/forms")]
public class ContentPage
{
[XmlElement(ElementName = "ContentPage.BindingContext", Namespace = "http://xamarin.com/schemas/2014/forms")]
public BindingContext BindingContext { get; set; }
}
public class BindingContext
{
[XmlElement(Namespace = "clr-namespace:MyProject.ViewModels")]
public string PointsPageViewModel { get; set; }
}
}