【问题标题】:Issue with Xamarin Sliders and restoring application stateXamarin 滑块问题和恢复应用程序状态
【发布时间】:2020-09-08 21:23:32
【问题描述】:

Xamarin 和整个移动平台的新东西,我也有一段时间没有开发了。但是,话虽如此,我认为除了当前的问题之外,我已经完成了很多工作。

我有一个网格表单,上面有多个滑块控件,全部设置为 0 到 15。它们工作得很好,问题是当我尝试保存和恢复我的应用程序属性时。保存和恢复第一个滑块很好(以及其他控件)。当我添加第二个或第三个时,它们都会得到最后一个滑块的值。示例 - 如果 Slider 1 = 5、Slider 2 = 10 和 Slider 3 = 15 在读取属性后启动,则所有滑块的值为 15。

我已经多次检查了我的变量、控件名称和属性。每个滑块的名称都不同。

const string p1s1secscore = "0";
const string p1s2secscore = "0";
    
public string P1S1SecScore { get; set; }
public string P1S2SecScore { get; set; }
    
    
if (Properties.ContainsKey(p1s1secscore))
{
   P1S1SecScore = (string)Properties[p1s1secscore];
}
if (Properties.ContainsKey(p1s2secscore))
{
     P1S2SecScore = (string)Properties[p1s2secscore];
}
    
protected override void OnAppearing()
{
   base.OnAppearing(); 
   p1S1Slider.Value = ToInt32((Application.Current as App).P1S1SecScore);
   p1S2Slider.Value = ToInt32((Application.Current as App).P1S2SecScore);
}

我什至明确设置了两个值(如下)来测试。恢复时,两个滑块的值均为 15。

   (Application.Current as App).P1S1SecScore = "10";
   (Application.Current as App).P1S2SecScore = "15";

有什么想法吗?

【问题讨论】:

    标签: c# xamarin slider state


    【解决方案1】:

    您将这些用作属性字典的键

    const string p1s1secscore = "0";
    const string p1s2secscore = "0";
    

    两者都是相同的值0,所以这个

    if (Properties.ContainsKey(p1s1secscore))
    {
       P1S1SecScore = (string)Properties[p1s1secscore];
    }
    if (Properties.ContainsKey(p1s2secscore))
    {
         P1S2SecScore = (string)Properties[p1s2secscore];
    }
    

    将始终从您的字典中检索相同的键

    【讨论】:

    • 是的,我只是想出了一种不同的声明方式(已发布),效果很好。
    • Jason - 哦,我明白你在说什么。谢谢,以后会牢记这一点。
    【解决方案2】:

    嗯,找到了一个不同的例子,它就像一个冠军。为我的所有属性使用这种类型的结构:

    if (Application.Current.Properties.ContainsKey("P1S1Scoring"))
    {
       p1S1Score.Text = Application.Current.Properties["P1S1Scoring"].ToString();
       p1S1Slider.Value = ToInt32(p1S1Score.Text);
    }
    

    我猜这是显式设置属性,而不使用 app.xmal.cs 中的“const”和覆盖的构造函数

    【讨论】:

    • 你原来的做法没有错,你只需要确保每个 const 都设置为唯一值
    猜你喜欢
    • 1970-01-01
    • 2015-04-25
    • 2013-02-24
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多