微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在一个页面中创建控件,这些控件有自己的其他控件。 我正在尝试将一个框架的内容绑定到另一个绑定内容中,但如果我尝试第二次访问它,它就会崩溃。

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

Xamarin 表单:5.0.0.2012

Xamarin.Essentials:1.6.1

PropertyChanged.Fody:3.3.2

主 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: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,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://imgur.com/a/caN9gxX

解决方法

我可能已经找到了解决方案。提示在 https://github.com/xamarin/Xamarin.Forms/issues/2713 中。

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


        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 };
        });

应用不会崩溃。

如果我连续触发命令,空内容很重要。它可能可以替换为 bool,测试用户是否多次触发命令,但这意味着更多的测试。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。