我想用Xamarin.Form和MVVM开发一个简单的项目.在我的解决方案(名为XamarinPOC)中,我(除了标准的Xamarin.Forms项目)还有一个用于模型的单独项目(XamarinPOC.Model)和一个用于ViewModel的单独项目(XamarinPOC.ViewModel).
我在XamarinPOC.ViewModel项目中定义了一个BaseViewModel类的抽象类(实现了INotifyPropertyChanged接口),之后我创建了一个使用简单属性扩展BaseViewModel类的SummaryViewModel类:
namespace XamarinPOC.ViewModel
{
public class SummaryViewModel : BaseViewModel
{
private string _test = "The binding is OK!";
public String test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("test");
}
}
public SummaryViewModel(){}
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,我在XamarinPOC项目中创建了一个简单的ContentPage(SummatyView),它只包含我想要显示ViewModel中定义的文本的标签.我想使用XAML来定义视图和绑定,但是当我运行应用程序时没有显示任何内容,我在编译时和运行时没有错误但是文本没有显示.我的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:local="clr-namespace:XamarinPOC.ViewModel,assembly=XamarinPOC.ViewModel"
x:Class="XamarinPOC.Summary"
Title="Summary List"
BindingContext="XamarinPOC.ViewModel.SummaryViewModel">
<StackLayout>
<Label Text="{Binding test}"/>
</StackLayout>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
最后我的app.cs是:
namespace XamarinPOC
{
public class App : Application
{
public App()
{
MainPage = new Summary();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在XamarinPOC项目中,我添加了对XamarinPOC.ViewModel和XamarinPOC.Model程序集的引用. …
<ViewCell>
<ViewCell.View>
<Label Text="{Binding ABC}"></Label>
</ViewCell.View>
</ViewCell>
Run Code Online (Sandbox Code Playgroud)
假设此视单元位于ListView中。如果内容页面与视图模型绑定,那么如何获得对内容页面绑定的引用。当前,“ ABC”正在引用列表中对象的属性,但我想从内容页面的绑定上下文中获取值。
<ffimageloading:CachedImage.GestureRecognizers>
<TapGestureRecognizer BindingContext="{x:Reference page}" Command="{Binding OnSignInCommand}" CommandParameter="{Binding Model}" />
</ffimageloading:CachedImage.GestureRecognizers>
Run Code Online (Sandbox Code Playgroud) 我正在使用 FreshMVVM 框架实现 Xamarin 应用程序,我想使用 BasePage 在页面之间共享一些代码。问题是,当我需要在 MainPage.xaml 中绑定某些属性时,我必须以这种方式指定源才能使其正常工作:Text="{Binding Title, Source={x:Reference mainPage}}"。否则没有源绑定就不起作用。好的,我明白了,但是这是正确的方法吗?还有其他方法可以达到相同的结果吗?当页面中有大量绑定时怎么办?例如,是否可以在上层“设置”源,因为在我看来,为每个绑定设置相同的源是非常烦人的。
基本页.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestXamarin.BasePage"
x:Name="basePage">
<ContentView>
<StackLayout Orientation="Vertical">
<Label Text="HEADER" FontSize="Large"/>
<Label Text="{Binding Text, Source={x:Reference basePage}}" FontSize="Large"/>
<ContentPresenter BindingContext="{Binding Parent.BindingContext}"
Content="{Binding PageContent, Source={x:Reference basePage}}" />
</StackLayout>
</ContentView>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
基本页.xaml.cs
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TestXamarin
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BasePage : ContentPage
{
public static readonly BindableProperty TextProperty = BindableProperty.Create(
nameof(Text),
typeof(string),
typeof(BasePage));
public string Text
{
get { return …Run Code Online (Sandbox Code Playgroud)