如何在OnNavigatedTo中正确设置CheckBox控件的状态

时间:2014-02-06 12:25:02

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

我在设置页面上的控件状态时遇到问题。例如,我有一个复选框,我想根据它之前的值设置其检查状态,我可以做得很好,但每次执行此操作时都会运行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();
            }
        }
    }

除了上述问题之外,我的实施工作很有效。当页面导航到?时,如何解决这些事件?

2 个答案:

答案 0 :(得分:0)

如果您不希望每次检查/取消选中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
   }
}

答案 1 :(得分:0)

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

只在页面cs class

上声明一个全局变量
    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。