Windows Phone 8.1 StorageFile Shopping 购物清单

network | | 访问(41)

MainPage.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,35,0,28">
        <TextBlock Text="我的应用程序" FontSize="20"  />
        <TextBlock Text="购物清单" FontSize="60" />
    </StackPanel>
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox FontSize="48" x:Name="Files"></ListBox>
    </Grid>
</Grid>
<Page.BottomAppBar>
    <CommandBar>
        <AppBarButton Label="新增" Icon="Add" Click="AppBarButton_Click"/>
    </CommandBar>
</Page.BottomAppBar>
public sealed partial class MainPage: Page {
    public MainPage() {
        InitializeComponent();
        Loaded += MainPage_Loaded;
    }

    async void MainPage_Loaded(object sender, RoutedEventArgs e) {
        Files.Items.Clear();
        StorageFolder storage = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ShoppingList", CreationCollisionOption.OpenIfExists);
        var files = await storage.GetFilesAsync(); {
            foreach(StorageFile file in files) {
                Grid a = new Grid();
                ColumnDefinition col = new ColumnDefinition();
                GridLength gl = new GridLength(200);
                col.Width = gl;
                a.ColumnDefinitions.Add(col);
                ColumnDefinition col2 = new ColumnDefinition();
                GridLength gl2 = new GridLength(200);
                col2.Width = gl;
                a.ColumnDefinitions.Add(col2);
                TextBlock txbx = new TextBlock();
                txbx.Text = file.DisplayName;
                Grid.SetColumn(txbx, 0);
                HyperlinkButton btn = new HyperlinkButton();
                btn.Width = 200;
                btn.Content = "查看详细";
                btn.Name = file.DisplayName;
                btn.Click += (s, ea) = >{
                    Frame.Navigate(typeof(DisplayPage), file);
                };
                Grid.SetColumn(btn, 1);

                a.Children.Add(txbx);
                a.Children.Add(btn);

                Files.Items.Add(a);
            }
        }
    }

    private void AppBarButton_Click(object sender, RoutedEventArgs e) {
        Frame.Navigate(typeof(AddItem));
    }
}

AddItem.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,35,0,28">
        <TextBlock Text="我的应用程序" FontSize="20"  />
        <TextBlock Text="添加商品" FontSize="60" />
    </StackPanel>
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="90" />
            <RowDefinition Height="90" />
            <RowDefinition Height="90" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100*" />
            <ColumnDefinition Width="346*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0" Grid.Row="0" Text="名称:" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBox Name="nameTxt"  Grid.Column="1" Grid.Row="0" Height="50"/>

        <TextBlock Grid.Column="0" Grid.Row="1" Text="价格:"  HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBox x:Name="priceTxt" Grid.Column="1" Grid.Row="1" Height="50" />

        <TextBlock Grid.Column="0" Grid.Row="2" Text="数量:" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBox Name="quanTxt" Grid.Column="1" Grid.Row="2" Height="50"/>


        <Button x:Name="BtnSave" Content="保存"   Width="370" Grid.Row="3" Grid.ColumnSpan="2" VerticalAlignment="Top" Click="BtnSave_Click" />
    </Grid>
</Grid>
public sealed partial class AddItem: Page {
    public AddItem() {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e) {}

    private async void BtnSave_Click(object sender, RoutedEventArgs e) {
        StorageFolder storage = await ApplicationData.Current.LocalFolder.GetFolderAsync("ShoppingList");
        XmlDocument _doc = new XmlDocument();
        XmlElement _item = _doc.CreateElement(nameTxt.Text);
        _item.SetAttribute("price", priceTxt.Text);
        _item.SetAttribute("quantity", quanTxt.Text);
        _doc.AppendChild(_item);
        StorageFile file = await storage.CreateFileAsync(nameTxt.Text + ".xml", CreationCollisionOption.ReplaceExisting);
        await _doc.SaveToFileAsync(file);
        Frame.GoBack();
    }
}

DisplayPage.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,35,0,28">
        <TextBlock x:Name="PageTitle" Text="" FontSize="20"  />
        <TextBlock Text="商品信息" FontSize="60" />
    </StackPanel>
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.3*" />
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Grid.Row="0" Text="名称:" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBlock Name="nameTxt"  Grid.Column="1" Grid.Row="0" FontSize="30"/>
        <TextBlock Grid.Row="1" Text="价格:" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBlock x:Name="priceTxt"  Grid.Column="1" Grid.Row="1" FontSize="30" />
        <TextBlock Grid.Column="0" Grid.Row="2" Text="数量:" HorizontalAlignment="Center" VerticalAlignment="Center" />
        <TextBlock Name="quanTxt" Grid.Column="1" Grid.Row="2" FontSize="30"/>
    </Grid>
</Grid>
public sealed partial class DisplayPage: Page {
    public DisplayPage() {
        this.InitializeComponent();
    }

    protected async override void OnNavigatedTo(NavigationEventArgs e) {
        StorageFile file = e.Parameter as StorageFile;
        if (file == null) return;
        String itemName = file.DisplayName;
        PageTitle.Text = itemName;
        XmlDocument doc = await XmlDocument.LoadFromFileAsync(file);
        priceTxt.Text = doc.DocumentElement.Attributes.GetNamedItem("price").NodeValue.ToString();
        quanTxt.Text = doc.DocumentElement.Attributes.GetNamedItem("quantity").NodeValue.ToString();
        nameTxt.Text = itemName;
    }

}