首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将内容绑定到Xamarin.UWP中的其他内容中?

如何将内容绑定到Xamarin.UWP中的其他内容中?
EN

Stack Overflow用户
提问于 2021-03-04 11:25:06
回答 1查看 246关注 0票数 0

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

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

Xamarin形式: 5.0.0.2012

Xamarin.Essentials: 1.6.1

PropertyChanged.Fody: 3.3.2

Main Xaml ->

代码语言:javascript
复制
<?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-->

代码语言:javascript
复制
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
    }
}

主要型号-->

代码语言:javascript
复制
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
    }
}

第一内容视图-->

代码语言:javascript
复制
<?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>

第二内容

代码语言:javascript
复制
<?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

  • 1图像是启动
  • 2图像是在按下顶部按钮后
  • 第3图像按下
  • 4图像后的错误堆栈跟踪,再次按下顶部按钮后

F 225

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-05 14:08:36

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

如果我像这样修改视图模型中的命令ChangeToOtherContent和ChangeToContent:

代码语言:javascript
复制
        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取代,测试用户是否多次触发命令,但这意味着要进行更多的测试。

我不明白为什么需要添加绑定上下文,因为它在第一次呈现时是正确的。也许有人能再加进去。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66474059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档