【问题标题】:How to Properly Set the State of a CheckBox Control in OnNavigatedTo如何在 OnNavigatedTo 中正确设置 CheckBox 控件的状态
【发布时间】:2014-02-06 12:25:02
【问题描述】:

我在设置页面上的控件状态时遇到问题。例如,我有一个复选框,我想根据之前的值设置它的选中状态,我可以做得很好,但是每次我这样做时都会运行 Checked click 事件。在这种情况下,这是一个问题,因为我有一个始终显示的 CustomMessageBox。无论页面是从另一个页面导航到,还是页面从墓碑恢复,都会发生这种情况。如何解决此问题以直观地显示视图中的状态但不运行与其关联的事件?

Page.xaml

<CheckBox x:Name="CheckBox" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked">
                        <CheckBox.Content>
                            <TextBlock Text="checked or not checked" FontSize="20" TextWrapping="Wrap"/>
                        </CheckBox.Content>
                    </CheckBox>

Page.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        //Set the checked state of the Checkbox when page is navigated to
        if (Settings.CheckBoxEnabled.Value == true)
            CheckBox.IsChecked = true;
        else
            CheckBox.IsChecked = false;

    }

private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        var cb = sender as CheckBox;

        if (cb.IsChecked.HasValue)
        {
            if (cb.IsChecked.Value == true)
            {
                Settings.CheckBoxEnabled.Value = true;

                //..code that calls another method which also shows a CustomMessageBox
            }
        }
    }

    private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        var cb = sender as CheckBox;

        if (cb.IsChecked.HasValue)
        {
            if (cb.IsChecked.Value == false)
            {
                Settings.CheckBoxEnabled.Value = false;

                CustomMessageBox messageBox = new CustomMessageBox()
                {
                    Caption = "\n" + "Checkbox change!",
                    Message = "\n" + "You have changed the state of the checkbox." + "\n",
                    LeftButtonContent = "OK"
                };

                messageBox.Dismissed += (s1, e1) =>
                {
                    switch (e1.Result)
                    {
                        case CustomMessageBoxResult.LeftButton:
                            //Do nothing
                            break;
                        case CustomMessageBoxResult.None:
                            //Do nothing
                            break;
                        default:
                            //Do nothing
                            break;
                    }
                };

                messageBox.Show();
            }
        }
    }

除了上述问题外,我的实现效果很好。当页面导航到时,如何绕过这些事件触发?

【问题讨论】:

    标签: c# xaml windows-phone-7 windows-phone-8


    【解决方案1】:

    如果您不想在每次选中/取消选中 CheckBox 时触发事件,但希望在用户单击时触发事件 - 然后订阅 CheckBox.Click。代码可能如下所示:

    // in page Constuctor or in XAML:
    myCheckBox.Click += myCheckbox_Click;
    
    private void myCheckbox_Click(object sender, RoutedEventArgs e)
    {
       if (myCheckBox.IsChecked == true)
       {
          //do for check
       }
       else
       {
          //do for uncheck
       }
    }
    

    【讨论】:

      【解决方案2】:

      在 Navigation 上管理 Checked/unchecked 事件的一种方法(Heck)是在页面上使用全局 int 变量

      只需在页面cs类上声明一个全局变量

          int CheckFlag = 0;
      

      现在,在选中的事件中

      private void CycleTileCheckBox_Checked(object sender, RoutedEventArgs e)
          {
              if(checkFlag!=0)
              {
              var cb = sender as CheckBox;
              if (cb.IsChecked.HasValue)
              {
                  if (cb.IsChecked.Value == true)
                  {
                      Settings.CheckBoxEnabled.Value = true;
                      //..code that calls another method which also shows a CustomMessageBox
                  }
              }
            }
            else
            {checkFlag=1;}
          }
      

      不要忘记在 On NavigatedFrom 事件中将其设为 0。

      【讨论】:

        猜你喜欢
        • 2019-07-13
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 2020-08-23
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        • 1970-01-01
        相关资源
        最近更新 更多