如何在Xamarin.UWP中绑定另一个内容中的内容?



我在一个页面中创建控件,这些控件有它们自己的其他控件。我试图将一个框架的内容绑定到另一个绑定的内容中,但是当我第二次尝试访问它时它崩溃了。

还尝试将绑定模式更改为双向,结果相同。

Xamarin Forms: 5.0.0.2012

Xamarin的。要点:1.6.1

propertychange。Fody: 3.3.2

Main

<?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:test="clr-namespace:Test"
x:Class="Test.MainPage">
<ContentPage.BindingContext>
<test:MainViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Button Text="Some Content View"
Command="{Binding ChangeToContent}"/>
<Button Text="Some other Content View"
Command="{Binding ChangeToOtherContent}"
/>
<Frame Content="{Binding model.MainContent}"/>
</StackLayout>
</ContentPage>

MainViewModel——在

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using Xamarin.Forms;
namespace Test
{
public class MainViewModel : INotifyPropertyChanged
{
public MainModel model { get; set; } = new MainModel();
public Command ChangeToContent => new Command(() => {
model.MainContent.Content = new Test1Content();
});
public Command ChangeToOtherContent => new Command(() => {
model.MainContent.Content = new Test2Content();
});

#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}

主模型——>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using Xamarin.Forms;
namespace Test
{
public class MainModel : INotifyPropertyChanged
{
public Frame SomeContent { get; set; } = new Frame()
{
BackgroundColor = Color.Red,
WidthRequest = 40,
HeightRequest = 40
};
public Frame SomeOtherContent { get; set; } = new Frame()
{
BackgroundColor = Color.Blue,
WidthRequest = 40,
HeightRequest = 40
};
public ContentView MainContent { get; set; } = new ContentView();
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}

第一个内容视图——>

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.Test1Content">
<ContentView.Content>
<StackLayout>
<Label Text="This is Test 1 Content" />
<Frame Content="{Binding model.SomeContent}"/>
</StackLayout>
</ContentView.Content>
</ContentView>

第二内容

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.Test2Content">
<ContentView.Content>
<StackLayout>
<Label Text="This is Test 2 Content" />
<Frame Content="{Binding model.SomeOtherContent}"/>
</StackLayout>
</ContentView.Content>
</ContentView>

结果:https://i.stack.imgur.com/Ki2Y0.jpg

  • 第一张图片是启动
  • 第二张图片是按下顶部按钮后的图像
  • 第三张图片是在按下
  • 下的按钮后
  • 第4张图是再次按下顶部按钮后的错误堆栈轨迹

我可能找到解决办法了。提示在https://github.com/xamarin/Xamarin.Forms/issues/2713

如果我修改命令ChangeToOtherContent和changettocontent在我的Viewmodel中像这样:


public Command ChangeToContent => new Command(() => {
model.MainContent.Content = null;
model.MainContent.Content = new Test1Content() { BindingContext = this };
});
public Command ChangeToOtherContent => new Command(() => {
model.MainContent.Content = null;
model.MainContent.Content = new Test2Content() { BindingContext = this };
});

应用程序不会崩溃。

如果我连续触发命令,null内容很重要。它可能被一个bool值代替,测试用户是否多次触发命令,但这意味着需要更多的测试。

我不明白为什么需要添加bindingcontext,因为它第一次呈现时是正确的。也许有人可以补充一下。

相关内容

  • 没有找到相关文章

最新更新