【问题标题】:StorageFile contain data or not (windwos phone)StorageFile 是否包含数据(windwos phone)
【发布时间】:2017-05-09 22:17:51
【问题描述】:

如何获取 StorageFile 文件包含数据?如果不是那么

AreaTextBlock.Text = "1.8;"

我当前的代码是:

private async void Button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
      StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("text.txt", CreationCollisionOption.OpenIfExists);
      await FileIO.WriteTextAsync(file, "1.9");
}

private async void SettingsPage_Loaded(object sender, RoutedEventArgs e)
{
      StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("text.txt", CreationCollisionOption.OpenIfExists);
      AreaTextBlock.Text = await FileIO.ReadTextAsync(file);

}

点击按钮时,AreaTextBlock.Text 是 1.9,但我需要在从不点击按钮时,AreaTextBlock.Text 应该是 1.8

我不想在 xaml 或 c# 初始化中编写 AreaTextBlock.Text = "1.8",因为当退出并重新启动时,AreaTextBlock.Text 是 1.8,即使它的文本是 1.9。 那么该怎么做呢

【问题讨论】:

    标签: c# windows-phone-8.1 windows-phone-silverlight


    【解决方案1】:

    基本上,您只需要在Loaded 方法的末尾检查AreaTextBox.Text 是否为空,如果是,则设置默认值。

    if ( AreaTextBox.Text == "" ) 
    {
          AreaTextBox.Text = "1.8";
    } 
    

    【讨论】:

    • 哈哈我在 AreaTextBlock.Text = await FileIO.ReadTextAsync(file);这就是为什么它现在不起作用的原因,谢谢..
    猜你喜欢
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    相关资源
    最近更新 更多