Cache Trang để truyền dữ liệu trong Windows store application


Khi ta tạo một Ứng dụng Windows Store, nó có thể chứa nhiều trang để điều hướng từ trang này sang trang khác trong ứng dụng. Trong ứng dụng Windows store, bạn cũng có thể truyền giá trị thông qua các trang khác nhau trên một trang web. Nhưng trong quá trình điều hướng giữa các trang khác nhau trong một ứng dụng, bạn có thể nhận thấy rằng khi bạn điều hướng từ trang này sang trang khác, khi bạn quay lại trang trước đó tất cả các trường trong trang đã bị mất.

Để giải quyết vấn đề này chúng ta phải cache nội dung của trang, để giữ dữ liệu của trang trong quá trình chuyển hướng giữa các trang trong một ứng dụng.

Bạn có thể cache trang bằng cách chỉ định thuộc tính NavigationCacheMode của nó trong hàm tạo của trang, và đặt NavigationCacheMode của nó thành Enabled. Thao tác này sẽ lưu trữ thông tin của trang khi lưu các trang trong Ứng dụng Windows Store Application. 

Các bước thực hiện:
Bước 1 Để tạo một Ứng dụng Windows mới:

  • Mở Visual Studio 2012.
  • Chọn File> New Project. Hộp thoại New Project mở ra.
  • Chọn kiểu mẫu Windows Store Application.
  • Chọn blank app.
  • Nhập tên cho dự án. Trong chủ đề này, chúng ta sẽ gọi dự án là Chachedpage.
  • Nhấp OK. Các tệp dự án của bạn được tạo ra.
private void click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(BlankPage2));
}
private void bindData_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(BlankPage1));
}
Bây giờ, chúng ta phải thay đổi phương thức OnLaunched trong tệp app.xaml.cs để làm cho BlankPage1 xuất hiện khi ứng dụng bắt đầu:
x



Bước 2, thêm trang mới

Mã XAML của Page 1 (chuyển hướng đến trang 2)



Code của page 2 xaml
Tạo sự kiện click để chuyển hướng
Sự kiện quay lại trang 1 từ trang 2
Để cache trang, ta dùng đoạn code sau trong constructor (hàm tạo) của BlankPage1
public BlankPage1()
 {
      this.InitializeComponent();
      this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;   
 }
Sau đó đổi hàm Onlaunched trong file app.xaml.cs
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
    // Create a Frame to act navigation context and navigate to the first page
    var rootFrame = new Frame();
    rootFrame.Navigate(typeof(blankpage1));

    // Place the frame in the current window and ensure that it is active
    Window.Current.Content = rootFrame;
    Window.Current.Activate();

}

Giờ thì chạy ứng dụng và xem kết quả !
Theo c-sharpcorner

Tạo máy tính - caculator cơ bản trong windows store application


Giới thiệu
Hôm nay chúng ta sẽ làm một máy tính đơn giản trong Windows Store App sử dụng C# và XAML trong Visual Studio 2012. Ứng dụng máy tính này sẽ đơn giản thực hiện tính toán cộng, trừ, nhân, chia và chia lấy dư như Windows Calculator .
Bước 1 ta tạo 1 app mới đã



Ta vào sửa file MainPage.xaml

Nội dung file như sau:


 
  

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
 
namespace App2
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ValuText.IsReadOnly = true;
        }
         string opration;
        double num1, num2, result;
        private void Btn1_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "1";
        }
        private void Btn2_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "2";
        }
        private void Btn3_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "3";
        }
        private void Btn4_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "4";
        }
        private void Btn5_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "5";
        }
        private void Btn6_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "6";
        }
        private void Btn7_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "7";
        }
        private void Btn8_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "8";
        }
        private void Btn9_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "9";
        } 
        private void Btn0_Click(object sender, RoutedEventArgs e)
        {
            TextBox.Text = TextBox.Text + "0";
        }
        private void BtnDot_Click(object sender, RoutedEventArgs e)
        {
            if(TextBox.Text.Contains("."))
            {
                TextBox.Text=TextBox.Text;
            }
            else
            {
                TextBox.Text=TextBox.Text+".";
            }
        }
        private void BtnPlus_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox.Text.Length > 0)
            {
                num1 = double.Parse(TextBox.Text);
                opration = "+";
                TextBox.Text = "";
                ValuText.Text = num1 + "+";
            }
        }
        private void BtnMinus_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox.Text.Length > 0)
            {
                num1 = double.Parse(TextBox.Text);
                opration = "-";
                TextBox.Text = "";
                ValuText.Text = num1 + "-";
            }
        }
        private void BtnMulti_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox.Text.Length > 0)
            {
                num1 = double.Parse(TextBox.Text);
                opration = "*";
                TextBox.Text = "";
                ValuText.Text = num1 + "*";
            }
        }
        private void BtnDiv_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox.Text.Length > 0)
            {
                num1 = double.Parse(TextBox.Text);
                opration = "/";
                TextBox.Text = "";
                ValuText.Text = num1 + "/";
            }
        }
        private void BtnPer_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox.Text.Length > 0)
            {
                num1 = double.Parse(TextBox.Text);
                opration = "%";
                TextBox.Text = "";
                ValuText.Text = num1 + "%";
            }
        } 
        private void BtnCan_Click(object sender, RoutedEventArgs e)
        {
            int length = TextBox.Text.Length;
            if (length > 0)
            {
                TextBox.Text = TextBox.Text.Remove(length - 1);
            }
            else
            {
                TextBox.Text = TextBox.Text;
            }
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
                TextBox.Text = "";
                ValuText.Text = "";
        } 
        private void BtnEqual_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox.Text.Length > 0)
            {
                num2 = double.Parse(TextBox.Text);
                ValuText.Text = "";
                switch (opration)
                {
                    case "+":
                        result = num1 + num2;
                        TextBox.Text = result.ToString();
                        break;
                    case "-":
                        result = num1 - num2;
                        TextBox.Text = result.ToString();
                        break;
                    case "*":
                        result = num1 * num2;
                        TextBox.Text = result.ToString();
                        break;
                    case "/":
                        if (num2 == 0)
                        {
                            ValuText.Text = "Cannot divide by zero";
                            break;
                        }
                        else
                        {
                            result = num1 / num2;
                            TextBox.Text = result.ToString();
                            break;
                        }
                    case "%":
                        num1 = num1 * num2;
                        result = num1 / 100;
                        TextBox.Text = result.ToString();
                        break;
                }
            }
        }    
    }
}
Kết quả