Skip to content

Instantly share code, notes, and snippets.

@hhyyg
Last active January 19, 2017 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhyyg/be9afece70b5f324f43872e273b292d8 to your computer and use it in GitHub Desktop.
Save hhyyg/be9afece70b5f324f43872e273b292d8 to your computer and use it in GitHub Desktop.
[AndroidHandsOn · P3PPP/NakayokunaruHandsOn Wiki](https://github.com/P3PPP/NakayokunaruHandsOn/wiki/AndroidHandsOn) こちらのハンズオン前半を、Prism で Bindable にした場合のコード。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace App.Controls
{
public class RoundedBoxView : View
{
#region CornerRadius BindableProperty
public static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create(
nameof(CornerRadius),
typeof(double),
typeof(RoundedBoxView),
5.0,
defaultBindingMode: BindingMode.TwoWay);
//ViewModelの値が変更されたら、を有効にするためTwoWay
public double CornerRadius
{
get {
System.Diagnostics.Debug.WriteLine("RoundedBoxView CornerRadius Get");
return (double)GetValue(CornerRadiusProperty);
}
set {
System.Diagnostics.Debug.WriteLine("RoundedBoxView CornerRadius Set");
SetValue(CornerRadiusProperty, value);
}
}
#endregion
#region Color BindableProperty
public static readonly BindableProperty ColorProperty =
BindableProperty.Create(
nameof(Color),
typeof(Color),
typeof(RoundedBoxView),
Color.Accent);
public Color Color
{
get { return (Color)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
#endregion
public static readonly BindableProperty ClickedProperty =
BindableProperty.Create(
nameof(Command),
typeof(ICommand),
typeof(RoundedBoxView));
public ICommand Clicked
{
get { return (ICommand)GetValue(ClickedProperty); }
set { SetValue(ClickedProperty, value); }
}
/// <summary>
/// ネイティブ コントロールから呼び出される用
/// </summary>
internal void SendClick()
{
if (Clicked != null)
Clicked.Execute(parameter: null);
}
}
}
using Prism.Mvvm;
using System.Diagnostics;
namespace App.ViewModels
{
public class HandsOnRoundedBoxViewPageViewModel : BindableBase
{
public HandsOnRoundedBoxViewPageViewModel()
{
this.ClickCommand = new Prism.Commands.DelegateCommand(OnClick);
}
public Prism.Commands.DelegateCommand ClickCommand { get; }
private int myCornerRadius = 20;
public int MyCornerRadius
{
get {
Debug.WriteLine("HandsOnRoundedBoxViewPageViewModel " + "MyCornerRadius Get ");
return this.myCornerRadius;
}
set
{
Debug.WriteLine("HandsOnRoundedBoxViewPageViewModel " + "MyCornerRadius Set " + value);
this.SetProperty(ref this.myCornerRadius, value);
}
}
private void OnClick()
{
Debug.WriteLine("HandsOnRoundedBoxViewPageViewModel " + "OnClick()");
this.MyCornerRadius = 40;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App.Views.HandsOnRoundedBoxViewPage"
xmlns:local="clr-namespace:App.Controls"
xmlns:mvvm="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
mvvm:ViewModelLocator.AutowireViewModel="true" >
<StackLayout HorizontalOptions="Center"
VerticalOptions="Center">
<local:RoundedBoxView x:Name="roundedBox"
HeightRequest="100"
WidthRequest="100"
CornerRadius="{Binding MyCornerRadius}"
Clicked="{Binding ClickCommand}"/>
<Button Text="Next Color" />
</StackLayout>
</ContentPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment