无法将XAML组件字段绑定到C#属性以刷新GUI [英] Unable to bind XAML component field to C# property to refresh GUI

查看:51
本文介绍了无法将XAML组件字段绑定到C#属性以刷新GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我已经尝试(并且到目前为止失败)将我的XAML连接到C#源,以便GUI更新(在更改源属性时)。我发现了一些很好的文章( Example1

Example2
),但无法获得上班。我真的很感激一些帮助。我正在尝试实现一个简单的流程,其中主页面具有TextBlock和设置页面的菜单。设置页面有一个更新属性的CheckBox。我的意图
是单击设置页面上的CheckBox将更改此属性并触发一个事件,以便主页上的TextBlock得到更新。

Hi. I've tried (and so far failed) to hook up my XAML to the C# source such that the GUI updates (upon a change to the source property). I've found some good write-ups (Example1 or Example2), but can't get it to work. I would really appreciate some help on this. I'm trying to implement a simple flow where the main page has a TextBlock and a menu to a settings page. The settings page has a CheckBox that updates a property. My intent is that clicking the CheckBox on the settings page will change this property and fire an event such that the TextBlock on the main page gets updated.

这是我的意思到目前为止尝试过:

Here's what I've tried so far:

MainPage.xaml

<phone:PhoneApplicationPage
    x:Class="SimpleHelloWorld.MainPage"
    xmlns:local="clr-namespace:SimpleHelloWorld"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <local:AppSettings x:Key="appSettings"/>
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <TextBlock HorizontalAlignment="Left" Height="64" Margin="34,37,0,0" TextWrapping="Wrap" Text="{Binding Source={StaticResource appSettings}, Path=CheckBoxState, Mode=OneWay}" VerticalAlignment="Top" Width="224" FontSize="40"/>
    </Grid>

    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="Settings" Click="NavigateToSettings"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

</phone:PhoneApplicationPage>


MainPage.xaml.cs

namespace SimpleHelloWorld {
    public partial class MainPage : PhoneApplicationPage {
        // Constructor
        public MainPage() {
            InitializeComponent();
        }

        private void NavigateToSettings(object sender, EventArgs e) {
            NavigationService.Navigate(new Uri("/Settings.xaml", UriKind.Relative));
        }

        public string CheckBoxState {
            get { return (string)GetValue(CheckBoxStateProperty); }
            set { SetValue(CheckBoxStateProperty, value); }
        }

        public static readonly DependencyProperty CheckBoxStateProperty =
          DependencyProperty.Register("CheckBoxState", typeof(bool), typeof(AppSettings),
          new PropertyMetadata(bool.FalseString));
    }
}

Settings.xaml

<phone:PhoneApplicationPage
    x:Class="SimpleHelloWorld.Settings"
    xmlns:local="clr-namespace:SimpleHelloWorld"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <local:AppSettings x:Key="appSettings"/>
    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <CheckBox Content="Checkbox" HorizontalAlignment="Left" Height="103" Margin="34,37,0,0" VerticalAlignment="Top" Width="245" FontSize="40"
                      IsChecked="{Binding Source={StaticResource appSettings}, Path=CheckBoxState, Mode=TwoWay}"
                      />
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

AppSettings.cs

using System.ComponentModel;

namespace SimpleHelloWorld {
    public class AppSettings : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
        private bool _state = true;

        public bool CheckBoxState {
            get {
                return _state;
            }
            set {
                _state = value;
                OnPropertyChanged(new PropertyChangedEventArgs("CheckBoxState"));
            }
        }

        public void OnPropertyChanged(PropertyChangedEventArgs e) {
            if (PropertyChanged != null) {
                PropertyChanged(this, e);
            }
        }
    }
}




推荐答案

John Connors1,

Hi John Connors1,

>>我正在尝试实现一个简单的流程,其中主页面具有TextBlock和设置页面的菜单。设置页面有一个更新属性的CheckBox。我的意图是单击设置页面上的CheckBox将更改此属性
并触发一个事件,以便主页上的TextBlock得到更新。

基于在您的描述中,您似乎想要绑定数据跨页面。我已经测试了你的代码,你的代码在一个页面中绑定时效果很好。在我看来,我认为它不起作用跨页可能是因为在页面导航期间,
页面可能由于页面生命周期而被删除,并且您已将数据绑定到AppSetting对象。因此,AppSetting创建一个新实例,并且
CheckBoxState 的值将始终为真。然后以这种方式虽然更新了CheckBox的值,但TextBlock仍会提醒相同的内容。

Based on your description, it seems that you want to bind data cross pages. I have tested your code, your code works well when bind in one page. In my mind, I guess it does not work cross pages may be because that during the page navigation, the page may has been dropped due to the page life cycle and you have bind the data to the AppSetting object. So the AppSetting creat a new instance and the value of the CheckBoxState will always be ture. Then in this way although the value of the CheckBox has been updated, the TextBlock will still remind the same.

如果你想更新数据跨页,我会建议你使用 IsolatedStorageSettings 保存数据更改。


欲了解更多信息,请尝试参考:


https://msdn.microsoft.com/en-us/library/windows/apps/jj714090(v = vs.105)。 aspx

If you want to update data cross pages, I will recommand you use the IsolatedStorageSettings to save data changes.
For more information, please try to refer to:

https://msdn.microsoft.com/en-us/library/windows/apps/jj714090(v=vs.105).aspx .

最好的问候,

Amy Peng

Best Regards,
Amy Peng


这篇关于无法将XAML组件字段绑定到C#属性以刷新GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆