Xamarin.Forms中的绑定



有一个代码,其本质如下:当你点击"Go"按钮,则应转换到新页面。此页面应显示图像和文本。文本和图像存储在单独的文件夹中,并标记为嵌入式资源。但问题是,当你点击按钮时,它不会在新页面上显示任何内容。

'

`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using MDivination.ViewModels;
namespace MDivination.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DivinationResult : ContentPage
{

public DivinationResult()
{
InitializeComponent();
BindingContext = new MemViewModel();

}
}
}

`

具有绑定的页面

'

<?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:models="clr-namespace:MDivination.Models" 
xmlns:viewmodels="clr-namespace:MDivination.ViewModels"
x:DataType="models:Mem"
x:Class="MDivination.Views.DivinationResult">
<ContentPage.BindingContext>
<viewmodels:MemViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>

<StackLayout>

<Image Source="{Binding  imageSource}" Aspect="AspectFit"  
VerticalOptions="Center"
HorizontalOptions="Center"
HeightRequest="100" WidthRequest="100"/>
<Label Text="{Binding interpretation}"
VerticalOptions="Center"
HorizontalOptions="Center"
TextColor="Purple"/>
</StackLayout>
</ContentPage.Content>


</ContentPage>
'

带有ViewModel的页面'

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Input;
using MDivination.Models;
using Xamarin.Forms;
using MDivination.Views;
using System.Runtime.InteropServices.ComTypes;
using System.Collections.ObjectModel;
using System.Reflection;
namespace MDivination.ViewModels
{
public class MemViewModel : INotifyPropertyChanged
{

public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public ICommand DivinationCommand { get; set; }
public ICommand BackCommand { get; set; }
public INavigation Navigation;
public Mem Mem { get; set; }
public MemViewModel()
{
Mem=new Mem();
DivinationCommand = new Command(Divination);
BackCommand = new Command(Back);
}

void Divination(object sender)
{

Navigation.PushAsync(new DivinationResult());
}
void Back(object sender)
{
Navigation.PopModalAsync();
}
}
}

'模型

using MDivination.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Xamarin.Forms;
namespace MDivination.Models
{
public class Mem : INotifyPropertyChanged
{

private int Id { get; set; }
private ImageSource ImageSource { get; set; }
private string Interpretation { get; set; }
private string History { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public Mem() { }
public int id
{
get { return id; }
set
{
Random random = new Random();
id = random.Next(0,2);
OnPropertyChanged(nameof(id));
}
}
public ImageSource imageSource
{
get { return imageSource; }
set {
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MemViewModel)).Assembly;
imageSource = ImageSource.FromResource($"MDivination.Images.{id}.png"); 
OnPropertyChanged(nameof(imageSource)); }
}
public string interpretation
{
get { return interpretation; }
set
{
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MemViewModel)).Assembly;
Stream streamInterpretation = assembly.GetManifestResourceStream($"MDivination.Interpretation.{id}.txt");
interpretation = new StreamReader(streamInterpretation).ReadToEnd();

OnPropertyChanged(nameof(interpretation));
}
}
public string history
{
get { return history; }
set
{
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MemViewModel)).Assembly;
Stream streamInterpretation = assembly.GetManifestResourceStream($"MDivination.History.{id}.txt");
history = new StreamReader(streamInterpretation).ReadToEnd(); 
History = history;
OnPropertyChanged(nameof(History));
}
}
}
}

这绑定到VM上的属性interpretation。但是您的虚拟机没有名为interpretation的属性

<Label Text="{Binding interpretation}"

相反,您的VM有一个名为Mem的属性,该属性有一个子属性interpretation。所以你的结合表达应该是

<Label Text="{Binding Mem.interpretation}"

最新更新