0

我正在尝试制作一个应用程序,而我只是想弄清楚 Windows 手机上的一切是如何工作的。

在 Windows Phone 模拟器中,我尝试运行我的应用程序,但它只是返回到前一个屏幕而没有任何错误。(编译器也没有给我任何错误。)

即使我除了 this.voice = value 没有放任何东西,它仍然不起作用。

这是发生错误的代码:

   // volume of the voice of the commentator;
   public int voice { 
        get 
        { 
            return voice; 
        }
        set 
        {
            settings["voice"] = this.voice = (int)value;  // right here it just stops.
        } 

我调用这个函数的代码是:

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
       // example :: ExceptionHandler.newException("er is geen exception");

        Option option = new Option();
        option.backgroundMusic = 22; // here
        option.voice = 32; // here

    }
}

而对于一个完整的图片:

主页 :

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
       // example :: ExceptionHandler.newException("er is geen exception");

        Option option = new Option();
        option.backgroundMusic = 22;
        option.voice = 32;

    }
}

类选项:

    public class Option
    {
    // isolated storage settings connection.
    private static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; 

    // volume of the backgroundMusic;
    public int backgroundMusic { 
        get 
        { 
            return backgroundMusic; 
        } 
        set 
        {
            settings["backgroundMusic"] = this.backgroundMusic = (int)value; 
        } 
    } 

    // volume of the voice of the commentator;
    public int voice { 
        get 
        { 
            return voice; 
        }
        set 
        {
            settings["voice"] = this.voice = (int)value; 
        } 
    }





    public Option()
    {
        // If the keys doesn't exists
        if (!settings.Contains("backgroundMusic") && !settings.Contains("voice"))
        {

            // Create the settings.
            settings.Add("backgroundMusic", (int)50 );
            settings.Add("voice", (int)50);
        }
        // If the key exists, retrieve the value and set the properties of backgroundMusic and voice
        else
        {
            this.backgroundMusic = (int)settings["backgroundMusic"];
            this.voice = (int)settings["voice"];
        }
    }
}

编辑 :

如果我做错了什么或者有什么更好的,我愿意接受建议,请告诉我。

4

1 回答 1

0

无限循环?this.voice = value再次触发集合,形成循环。

您必须声明另一个字段并将其用作值存储:

private int _voice;
public int voice
{ 
    get 
    { 
        return _voice; 
    }
    set 
    {
        settings["voice"] = _voice = (int)value; 
    } 
}

您也必须对第二个属性执行相同的操作。

于 2013-02-22T22:02:25.800 回答