Storing the state of a ToggleSwitch Into A Variable in C# and XAML

时间:2016-12-02 05:04:22

标签: c# xaml uwp uwp-xaml

EDIT : It's been solved. I slept on it and Bradley's comment helped me a ton! Thanks!

So I have a series of ToggleSwitches. Here's hows things are setup:

<ToggleSwitch
        x:Name="PrimaryToggle"
        Header="Primary"
        Toggled="PrimaryToggle_Toggled"/>

<ToggleSwitch
        x:Name="SecondaryToggle"
        Header="Secondary"
        Toggled="SecondaryToggle_Toggled"/>

<ToggleSwitch x:Name="Child1Toggle" Header="Child 1" />
<ToggleSwitch x:Name="Child2Toggle" Header="Child 2" />
<ToggleSwitch x:Name="Child3Toggle" Header="Child 3" />
<ToggleSwitch x:Name="Child4Toggle" Header="Child 4" />

In the code behind I have things setup like this:

// Parent Toggle (resets everything)
private void PrimaryToggle_Toggled(object sender, RoutedEventArgs e)
{
    if (PrimaryToggle.IsOn == true)
    {
        Child1Toggle.IsOn = false;
        Child2Toggle.IsOn = true;
        Child3Toggle.IsOn = false;
        Child4Toggle.IsOn = true;

    }
    else if (PrimaryToggle.IsOn == false)
    {
        this.allToggles(false);
    }
}

/* This is where I need to store the current state of the child toggles
   so when it turns off it will restore the saved state of them */
private void SecondaryToggle_Toggled(object sender, RoutedEventArgs e)
{
if (SecondaryToggle.IsOn == true)
    {
        /* Need to store current state of children toggles
           In this section here: */
        Child1Toggle.IsOn = true;
        Child2Toggle.IsOn = true;
        Child3Toggle.IsOn = false;
        Child4Toggle.IsOn = false;
    }

if (SecondaryToggle.IsOn == false && PrimaryToggle.IsOn == true)
    {
        /* Need to restore previous state of children 
       toggles in this section here: */
    }
}

So the problem is I'm pretty new to C# so I'm trying to track down the best way to handle this. Based on previous research I've come to the conclusion that I may need to use INotifyPropertyChanged and then set bindings on all the toggles I need to save, but I really do not know where to begin with that. I've done some { get; set; } stuff a while back but haven't used it in a while so I'm pretty much having a brainfart.

Any advice on where to begin?

Thanks!

1 个答案:

答案 0 :(得分:0)

所以这个解决方案并不是最优雅的,但是它确实有效,所以我们走了。

你需要在类中声明一些可以存储ToggleSwitches状态的bool:

public sealed partial class MainPage : Page
{
    public bool child1var { get; set; }
    public bool child2var { get; set; }
    public bool child3var { get; set; }
    public bool child4var { get; set; }

设置完毕后,您需要将数据设置在

之下
private void SecondaryToggle_Toggled(object sender, RoutedEventArgs e)
{
if (SecondaryToggle.IsOn == true)
    {
        // Here we save the state of the Child toggles 
        // before setting them to a different setting
        child1var = Child1Toggle.IsOn;
        child2var = Child2Toggle.IsOn;
        child3var = Child3Toggle.IsOn;
        child4var = Child4Toggle.IsOn;

        Child1Toggle.IsOn = true;
        Child2Toggle.IsOn = true;
        Child3Toggle.IsOn = false;
        Child4Toggle.IsOn = false;
    }

最后,当我们关闭辅助切换时,我们将其设置回原来的状态:

if (SecondaryToggle.IsOn == false && PrimaryToggle.IsOn == true)
    {
        Child1Toggle.IsOn = child1var;
        Child2Toggle.IsOn = child2var;
        Child3Toggle.IsOn = child3var;
        Child4Toggle.IsOn = child4var;
    }
}

这将数据设置回原来的状态。

希望这有助于其他人!再一次,我知道这可能不是最好的解决方案,并且可能有很好的方法将很多这些东西分类到其他类中,如果我很快就会学到这些,我一定会用这些信息来更新这篇文章。 !