Created
March 19, 2012 16:19
-
-
Save biac/2117818 to your computer and use it in GitHub Desktop.
Metro スタイルアプリで、DataContextへViewModelをバインドさせている
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// XAML | |
<Page | |
x:Class="FizzBuzzMetro.FizzBuzzPage" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="using:FizzBuzzMetro" | |
xmlns:vm="using:FizzBuzzModel" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
mc:Ignorable="d" | |
> | |
<Grid Background="{StaticResource ApplicationPageBackgroundBrush}"> | |
<Grid.DataContext> | |
<vm:FizzBuzzViewModel x:Name="ViewModel" /> | |
</Grid.DataContext> | |
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> | |
<TextBlock Text="{Binding FizzBuzzWord}" FontSize="96" FontFamily="Segoe UI Light" /> | |
<Button x:Name="NextButton" Content="Next" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="32" Margin="0,10,0,0" | |
Command="{Binding NextCommand}" FontWeight="Normal" FontFamily="Segoe UI Light" /> | |
</StackPanel> | |
</Grid> | |
</Page> | |
// 自動生成された .g.i.cs | |
public partial class FizzBuzzPage : Windows.UI.Xaml.Controls.Page | |
{ | |
private FizzBuzzModel.FizzBuzzViewModel ViewModel; | |
private Windows.UI.Xaml.Controls.Button NextButton; | |
private bool _contentLoaded; | |
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")] | |
[System.Diagnostics.DebuggerNonUserCodeAttribute()] | |
public void InitializeComponent() | |
{ | |
if (_contentLoaded) | |
return; | |
_contentLoaded = true; | |
Application.LoadComponent(this, new System.Uri("ms-appx:///FizzBuzzPage.xaml"), Windows.UI.Xaml.Controls.Primitives.ComponentResourceLocation.Application); | |
ViewModel = (FizzBuzzModel.FizzBuzzViewModel)this.FindName("ViewModel"); | |
NextButton = (Windows.UI.Xaml.Controls.Button)this.FindName("NextButton"); | |
} | |
} | |
// コードビハインド | |
public sealed partial class FizzBuzzPage : Page | |
{ | |
public FizzBuzzPage() | |
{ | |
this.InitializeComponent(); | |
} | |
/// <summary> | |
/// Invoked when this page is about to be displayed in a Frame. | |
/// </summary> | |
/// <param name="e">Event data that describes how this page was reached. The Parameter | |
/// property is typically used to configure the page.</param> | |
protected override void OnNavigatedTo(NavigationEventArgs e) | |
{ | |
} | |
} | |
// App.xaml | |
<Application | |
x:Class="FizzBuzzMetro.App" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="using:FizzBuzzMetro"> | |
<Application.Resources> | |
<ResourceDictionary> | |
<ResourceDictionary.MergedDictionaries> | |
<!-- | |
Styles that define common aspects of the platform look and feel | |
Required by Visual Studio project and item templates | |
--> | |
<ResourceDictionary Source="Common/StandardStyles.xaml"/> | |
</ResourceDictionary.MergedDictionaries> | |
</ResourceDictionary> | |
</Application.Resources> | |
</Application> | |
// App.xaml.cs | |
sealed partial class App : Application | |
{ | |
/// <summary> | |
/// Initializes the singleton application object. This is the first line of authored code | |
/// executed, and as such is the logical equivalent of main() or WinMain(). | |
/// </summary> | |
public App() | |
{ | |
this.InitializeComponent(); | |
this.Suspending += OnSuspending; | |
} | |
/// <summary> | |
/// Invoked when the application is launched normally by the end user. Other entry points | |
/// will be used when the application is launched to open a specific file, to display | |
/// search results, and so forth. | |
/// </summary> | |
/// <param name="args">Details about the launch request and process.</param> | |
protected override void OnLaunched(LaunchActivatedEventArgs args) | |
{ | |
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) | |
{ | |
//TODO: Load state from previously suspended application | |
} | |
// Create a Frame to act navigation context and navigate to the first page | |
var rootFrame = new Frame(); | |
rootFrame.Navigate(typeof(FizzBuzzPage)); | |
// Place the frame in the current Window and ensure that it is active | |
Window.Current.Content = rootFrame; | |
Window.Current.Activate(); | |
} | |
/// <summary> | |
/// Invoked when application execution is being suspended. Application state is saved | |
/// without knowing whether the application will be terminated or resumed with the contents | |
/// of memory still intact. | |
/// </summary> | |
/// <param name="sender">The source of the suspend request.</param> | |
/// <param name="e">Details about the suspend request.</param> | |
void OnSuspending(object sender, SuspendingEventArgs e) | |
{ | |
//TODO: Save application state and stop any background activity | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FizzBuzzPageのxamlを書いてるだけで、ViewModelがバインド出来ている。
ページビハインドやapp.xamlには、まったくノータッチ。