【问题标题】:XAML Data Binding value not refreshing inside two ContentPagesXAML 数据绑定值未在两个 ContentPages 内刷新
【发布时间】:2021-09-26 21:36:55
【问题描述】:

我正在尝试将我的对象中的值输出到两个不同的条目。两个条目都在同一个视图中,但在不同的 ContentPages 中,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="myApp.Views.ViewTabs.ViewHome"
            xmlns:localTabs="clr-namespace:myApp.Views.ViewTabs"
            xmlns:localObjPages="clr-namespace:myApp.Objects"
            >

    <ContentPage Title="PageOne">
        <ContentPage.BindingContext>
            <localObjPages:PagesObj/>
        </ContentPage.BindingContext>
        
        <ScrollView>
            <StackLayout>
                <Entry 
                x:Name="EntryOne" Text="{Binding BananaCount}"/>
                <Entry 
                x:Name="EntryTwo" Text="{Binding BananaCount}"/>                  
            </StackLayout>
        </ScrollView>
    </ContentPage>

<ContentPage Title="PageTwo">
        <ContentPage.BindingContext>
            <localObjPages:PagesObj/>
        </ContentPage.BindingContext>
        
        <ScrollView>
            <StackLayout>
                <Entry 
                x:Name="EntryThree" Text="{Binding BananaCount}"/>                  
            </StackLayout>
        </ScrollView>
    </ContentPage>
</TabbedPage>

我的模特:

public string BananaCount
        {
            get { return _bananaCount; }
            set
            {
                if (_bananaCount != value)
                {
                    _bananaCount = value;
                    NotifyPropertyChanged("BananaCount");
                }
            }
        }

当我在EntryOne 或EntryTwo 中更改对象时,该对象在EntryOne 或EntryTwo 中更新并返回。 但是EntryThree中没有更新。这是为什么呢?我正确绑定了吗?谢谢。

【问题讨论】:

  • PageTwo 正在使用它自己的 bindingcontext 副本,它不与其他页面共享相同的实例
  • 谢谢@Jason。显然我是编程新手。你会建议做什么?
  • 如果您希望所有页面使用相同的 BindingContext 实例,您可以改为在 TabbedPage 级别指定它
  • 谢谢。我尝试使用 但它不会显示子文件夹中的对象及其属性。出了点问题:/
  • 您的帖子信息不足,无法提出任何建议

标签: xaml xamarin data-binding


【解决方案1】:

当我在EntryOne 或EntryTwo 中更改对象时,该对象在EntryOne 或EntryTwo 中更新并返回。但是,它没有在 EntryThree 中更新。为什么是这样?我正确绑定了吗?

做一个关于TabbedPage的示例,为TabbedPage分配数据源,而不是contentpage,你可以看看:

<TabbedPage
x:Class="FormsSample.tabbedpage.TabbedPage6"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:FormsSample.tabbedpage">

  <!--Pages can be added as references or inline-->  
<ContentPage Title="PageOne">

    <ScrollView>
        <StackLayout>
            <Entry x:Name="EntryOne" Text="{Binding str}" />
            <Entry x:Name="EntryTwo" Text="{Binding str}" />
            <Button
                x:Name="btn1"
                Clicked="btn1_Clicked"
                Text="change data" />
        </StackLayout>
    </ScrollView>
</ContentPage>

<ContentPage Title="PageTwo">
    <ScrollView>
        <StackLayout>
            <Entry x:Name="EntryThree" Text="{Binding str}" />
        </StackLayout>
    </ScrollView>
</ContentPage>
 public partial class TabbedPage6 : TabbedPage
{
   public tabclass tabc { get; set; }
    public TabbedPage6()
    {
        InitializeComponent();
        tabc = new tabclass();
        this.BindingContext = tabc;
    }

    private void btn1_Clicked(object sender, EventArgs e)
    {
        tabc.str = "this is test!";
    }
}
public class tabclass:ViewModelBase
{
    private string _str;
    public string str
    { 
        get { return _str; } 
        set
        {
            _str = value;
            RaisePropertyChanged("str");
        }
    }
}

ViewModel 是一个实现 INotifyPropertyChanged 的​​类。

public class ViewModelBase : INotifyPropertyChanged
{
    
    public event PropertyChangedEventHandler PropertyChanged;

    
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-07
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多