I'm surprised that I'm unable to find any solution for capturing signature in Windows Phone 8.1. So far I found only one in StackOverflow here... but it complains that WriteableBitmap.DrawLine method isn't available.
Code from the above link:
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace ProofOfConcept.App.Controls
{
public class SignatureCaptureControl : Canvas
{
private WriteableBitmap _writeableBitmap;
private Point _currentPoint;
private Point _oldPoint;
public SignatureCaptureControl()
{
_writeableBitmap = new WriteableBitmap(300, 130);
PointerPressed += SignatureCaptureControl_PointerPressed;
PointerMoved += SignatureCaptureControl_PointerMoved;
}
private void SignatureCaptureControl_PointerPressed(object sender, PointerRoutedEventArgs e)
{
_currentPoint = e.GetCurrentPoint(this).Position;
_oldPoint = _currentPoint;
}
void SignatureCaptureControl_PointerMoved(object sender, PointerRoutedEventArgs e)
{
_currentPoint = e.GetCurrentPoint(this).Position;
_writeableBitmap.DrawLine((int)_currentPoint.X, (int)_currentPoint.Y, (int)_oldPoint.X, (int)_oldPoint.Y, PenColor);
this.InvalidateArrange();
_oldPoint = _currentPoint;
}
public void ClearSignature()
{
_writeableBitmap.Clear();
}
}
}
This code sample requires the WriteableBitmapEx library, the DrawLine method is defined as an extension method in that library.
You can install this package in NuGet package manager.
Finally I found a sample code that does exactly what I was looking for. It does let the user scribble their signature or whatever on the canvas, I'm yet to figure out how to save the image and upload it to a webservice. I'll get to that later. But here is the code to create a drawing pad.
Link to the website where I found the code.
I made it as a UserControl.
This is how I used the UserControl:
<Grid x:Name="inkPanel">
<Controls:SignatureCaptureControl/>
</Grid>
UserControl Xaml:
<UserControl
x:Class="MyApp.Controls.SignatureCaptureControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="170"></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<Canvas x:Name="inkCanvas" Grid.Row="0">
<Rectangle x:Name="rect" VerticalAlignment="Top" Height="150" Width="360" Fill="#FFF4F4F5" Stroke="Black" />
</Canvas>
<PasswordBox x:Name="txtPin" Grid.Row="1" Width="360" PlaceholderText="Enter Pin">
</PasswordBox>
<StackPanel Orientation="Horizontal" Grid.Row="2" Margin="0,10,0,0" HorizontalAlignment="Center">
<Button Name="btnAccept" Content="Accept" Click="btnAccept_Click"></Button>
<Button Name="btnClear" Content="Clear" Click="btnClear_Click"></Button>
</StackPanel>
</Grid>
</UserControl>
Code Behind:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.UI;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
namespace MyApp.Controls
{
public sealed partial class SignatureCaptureControl : UserControl
{
Windows.UI.Xaml.Shapes.Path _Current;
public SignatureCaptureControl()
{
this.InitializeComponent();
inkCanvas.PointerPressed += inkCanvas_PointerPressed;
inkCanvas.PointerMoved += inkCanvas_PointerMoved;
inkCanvas.PointerReleased += inkCanvas_PointerReleased;
inkCanvas.PointerExited += inkCanvas_PointerReleased;
}
void inkCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
_Current = null;
}
void inkCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
var pointer = e.GetCurrentPoint(inkCanvas);
if (!pointer.Properties.IsLeftButtonPressed || _Current == null)
return;
var segments = (_Current.Data as PathGeometry).Figures.First().Segments.First() as PolyLineSegment;
segments.Points.Add(pointer.Position);
}
void inkCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var pointer = e.GetCurrentPoint(inkCanvas);
if (!pointer.Properties.IsLeftButtonPressed)
return;
_Current = new Windows.UI.Xaml.Shapes.Path
{
Data = new PathGeometry
{
Figures = { new PathFigure { StartPoint = pointer.Position, Segments = { new PolyLineSegment() } } }
},
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 4d,
};
inkCanvas.Children.Add(_Current);
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
inkCanvas.Children.Clear();
inkCanvas.Children.Add(rect);
txtPin.Password = "";
}
private void btnAccept_Click(object sender, RoutedEventArgs e)
{
}
}
}
Related
I am trying to create a UWP feet/inches control that takes a value in inches, and splits it into two fields, feet and inches. The control, when updated by the user, should update the backend viewmodel with the new inches value.
Requirements
Change the value in 1, then the values in 2 and 3 update
accordingly.
Change the value in 3, the values in 1 and 2 update accordingly
The way I have this written it gets into an endless loop. I am not even sure the FeetInches control is written properly. How would I change this to meet the above requirements?
Downloadable/runnable code is available in GitHub, but here it is inline per SO guidelines...
App.xaml
<Application x:Class="UWPFeetInches.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWPFeetInches">
<Application.Resources>
<ResourceDictionary>
<local:NullDecimalConverter x:Key="NullDecimalConverter" />
</ResourceDictionary>
</Application.Resources>
</Application>
MainPage.xaml
<Page
x:Class="UWPFeetInches.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="using:UWPFeetInches"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
Text="Enter Inches Value:"
Margin="0,50,10,0"
VerticalAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="1"
Margin="0,50,10,0"
Text="{x:Bind ViewModel.Inches, Mode=TwoWay, Converter={StaticResource NullDecimalConverter}}" />
<TextBlock Grid.Row="1" Grid.Column="0"
Margin="0,50,10,0"
Text="Inches Display:"/>
<TextBlock Grid.Row="1" Grid.Column="1"
Margin="0,50,10,0"
Text="{x:Bind ViewModel.InchesDisplay, Mode=TwoWay}"/>
<TextBlock Grid.Row="2" Grid.Column="0"
Text="Feet / Inches Control:"
Margin="0,50,10,0"
VerticalAlignment="Center" />
<local:FeetInches Grid.Row="2" Grid.Column="1"
Margin="0,50,10,0"
Value="{x:Bind ViewModel.Inches, Mode=TwoWay}" />
</Grid>
</Page>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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 UWPFeetInches
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.ViewModel = new InchesViewModel();
}
public InchesViewModel ViewModel { get; set; }
}
public class InchesViewModel : INotifyPropertyChanged
{
private decimal? _Inches;
public decimal? Inches
{
get { return _Inches; }
set
{
if (value != _Inches)
{
_Inches = value;
InchesDisplay = _Inches == null ? "{null}" : _Inches.ToString();
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(Inches)));
}
}
}
private string _InchesDisplay;
public string InchesDisplay
{
get { return _InchesDisplay; }
set
{
if (value != _InchesDisplay)
{
_InchesDisplay = value;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(InchesDisplay)));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
NullDecimalConverter.cs
using System;
using Windows.UI.Xaml.Data;
namespace UWPFeetInches
{
public sealed class NullDecimalConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is decimal m)
{
return m == 0 ? "" : m.ToString();
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if (!string.IsNullOrWhiteSpace(value?.ToString()))
{
if (Decimal.TryParse(value.ToString(), out decimal m))
{
return m;
}
}
return null;
}
}
}
FeetInches.xaml
<UserControl
x:Class="UWPFeetInches.FeetInches"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:UWPFeetInches"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<StackPanel Orientation="Horizontal">
<TextBox Header="Feet"
Margin="0,0,10,0"
Text="{x:Bind Feet, Mode=TwoWay}" />
<TextBox Header="Inches"
Text="{x:Bind Inches, Mode=TwoWay, Converter={StaticResource NullDecimalConverter}}" />
</StackPanel>
</UserControl>
FeetInches.xaml.cs
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
namespace UWPFeetInches
{
public sealed partial class FeetInches : UserControl
{
public FeetInches()
{
this.InitializeComponent();
}
#region Feet
public int Feet
{
get { return (int)GetValue(FeetProperty); }
set
{
SetValue(FeetProperty, value);
}
}
public static readonly DependencyProperty FeetProperty = DependencyProperty.Register(nameof(Feet), typeof(int), typeof(FeetInches), new PropertyMetadata(0, OnPropertyChanged));
#endregion
#region Inches
public decimal Inches
{
get { return (decimal)GetValue(InchesProperty); }
set
{
SetValue(InchesProperty, value);
}
}
public static readonly DependencyProperty InchesProperty = DependencyProperty.Register(nameof(Inches), typeof(decimal), typeof(FeetInches), new PropertyMetadata(0M, OnPropertyChanged));
#endregion
#region Value
public decimal? Value
{
get { return (decimal)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(nameof(Value), typeof(decimal?), typeof(FeetInches), new PropertyMetadata(null, ValueOnPropertyChanged));
#endregion
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as FeetInches;
control.Value = control.Feet * 12 + control.Inches;
}
private static void ValueOnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as FeetInches;
var inches = control.Value;
control.Feet = inches.HasValue ? (int)(inches.Value / 12M) : 0;
control.Inches = inches.HasValue ? inches.Value - (control.Feet * 12M) : 0M;
}
}
}
When you subscribe the PropertyChanged event for the three dependency properties, it will get into an endless loop. You could try to subscribe the LostFocus event of TextBox in FeetInches.xaml to replace your OnPropertyChanged event for your Feet and Inches properties, in that case, it won't get into an endless loop. For example:
.xaml:
<TextBox Header="Feet"
Margin="0,0,10,0"
x:Name="MyFeet"
Text="{x:Bind Feet, Mode=TwoWay}" LostFocus="TextBox_LostFocus"/>
<TextBox Header="Inches"
x:Name="MyInches"
Text="{x:Bind Inches, Mode=TwoWay, Converter={StaticResource NullDecimalConverter}}" LostFocus="TextBox_LostFocus"/>
.cs:
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
var textbox = sender as TextBox;
Decimal.TryParse(textbox.Text, out decimal m);
if (textbox.Name.Equals("MyFeet"))
{
this.Value = m * 12 + this.Inches;
}
else
{
this.Value = this.Feet * 12 + m;
}
}
When triggered the LostFocus event, the property bound with the Text of TextBox has not changed, so you need to use the value of Text directly instead of the Feet/Inches property and you need to judge which TextBox triggers this event.
Or if you still want to use OnPropertyChanged event for Feet and Inches properties instead of LostFocus event, you can declare a property(e.g. bool valueChanged) to limit the calling of ValueOnPropertyChanged and OnPropertyChanged event.
.cs:
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (valueChanged == false) {
var control = d as FeetInches;
control.Value = control.Feet * 12 + control.Inches;
}
}
private static bool valueChanged = false;
private static void ValueOnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
valueChanged = true;
var control = d as FeetInches;
var inches = control.Value;
control.Feet = inches.HasValue ? (int)(inches.Value / 12M) : 0;
control.Inches = inches.HasValue ? inches.Value - (control.Feet * 12M) : 0M;
valueChanged = false;
}
Update:
You can use the Property property from DependencyPropertyChangedEventArgs data to check which dependency property changes.
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.Property == FeetProperty) {
//do something
}
else{
//do something
}
}
I have tried everything but I cannot get to update my observable collection and be reflected in the UI. I have a method that adds a new entry to the collection that is bound to a CollectionViewSource which is bound to my collection. When I run the application Universal App I get the list correctly but if I add a value on click nothing gets reflected. Any suggestions?
The XAML looks like this:
<SemanticZoom>
<SemanticZoom.ZoomedInView>
<ListView IsHoldingEnabled="True"
ItemsSource="{Binding Mode=OneWay, Source={StaticResource MenuGroups}}"
ItemTemplate="{StaticResource MenuItemTemplate}"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True" HeaderTemplate="{StaticResource MenuGroupHeaderTemplate}"/>
</ListView.GroupStyle>
</ListView>
</SemanticZoom.ZoomedInView>
<SemanticZoom.ZoomedOutView>
<GridView Background="Black"
ItemsSource="{Binding Source={StaticResource MenuGroups}, Path=CollectionGroups}"
ItemTemplate="{StaticResource MenuJumpTemplate}">
</GridView>
</SemanticZoom.ZoomedOutView>
</SemanticZoom>
Here is my code for the code behind:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
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;
using TestWin.TestWinService;
using System.Globalization;
using System.Collections.ObjectModel;
using System.ComponentModel;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace TestWin
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MenuPage : Page
{
#region Members
TestWinConnectionClient TestWinService;
ObservableCollection<Menu> ocMenuItems = null;
ObservableCollection<AlphaKeyGroup<Menu>> ocItemSource = null;
#endregion Members
#region Properties
public ObservableCollection<Menu> MenuItems
{
get
{
if (ocMenuItems == null)
{
ocMenuItems = new ObservableCollection<Menu>();
}
return ocMenuItems;
}
set
{
ocMenuItems = value;
OnPropertyChanged("MenuItems");
}
}
public ObservableCollection<AlphaKeyGroup<Menu>> ItemSource
{
get
{
if (ocItemSource == null)
{
ocItemSource = new ObservableCollection<AlphaKeyGroup<Menu>>((AlphaKeyGroup<Menu>.CreateGroups(MenuItems, CultureInfo.CurrentUICulture, s => s.MenuName, true)));
}
return ocItemSource;
}
set
{
ocItemSource = value;
OnPropertyChanged("ItemSource");
}
}
#endregion Properties
public MenuPage()
{
TestWinService = new TestWinConnectionClient();
this.InitializeComponent();
#region Events
this.Loaded += MenuPage_Loaded;
#endregion
}
private void MenuPage_Loaded(object sender, RoutedEventArgs e)
{
SetItemSource();
}
private async void SetItemSource()
{
MenuItems = await TestWinService.GetMenuEntriesAsync();
((CollectionViewSource)Resources["MenuGroups"]).Source = ItemSource;
}
private void TextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
Menu m = new Menu();
m.MenuName = "Test Entry";
m.SysRowID = Guid.NewGuid();
MenuItems.Add(m);
//this.Frame.Navigate(typeof(MenuPage));
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
If you add the new item to the MenuItems you also need to refresh the Source of CollectionViewSource.
Might be you have an ObservableCollection (ItemSource) that is not neccesary, you can take a look how I did it in Codeproject
and add for instance:
private async void Test()
{
await Task.Delay(5000);
for (int i = 0; i < 5; i++)
{
Favorites.Add(new Favorite()
{
Name = $"AAFriends {i}",
Category = new Category() { Name = "Friends" },
Uri = "http://www.expediteapps.com/blog/"
});
}
InitializeGrouping();
}
That refreshes only when I call the grouping again.
I'm a xaml novice and pretty new to WPF/Store apps. I tried to create a simple example of Caliburn Micro (compiled on the recent code) for a Windows 8.1 Store app. But I was unable to get it running as since yesterday I'm getting the error - I did tried the samples under the source I'd downloaded, they just work fine. Same I tried to create from scratch, it throws the aforementioned exception.
Here is the code of the entire solution, please correct me if I've configured/used it wrong!
CalMicSample\App.xaml:
<caliburn:CaliburnApplication
x:Class="CalMicSample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:caliburn="using:Caliburn.Micro"
RequestedTheme="Light">
</caliburn:CaliburnApplication>
CalMicSample\App.xaml.cs
using Caliburn.Micro;
using CalMicSample.ViewModels;
using CalMicSample.Views;
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml.Controls;
namespace CalMicSample
{
public sealed partial class App
{
private WinRTContainer container;
private INavigationService navigationService;
public App()
{
InitializeComponent();
}
protected override void Configure()
{
LogManager.GetLog = t => new DebugLog(t);
container = new WinRTContainer();
container.RegisterWinRTServices();
container.RegisterSharingService();
container
.PerRequest<MyTestViewModel>();
PrepareViewFirst();
}
protected override object GetInstance(Type service, string key)
{
var instance = container.GetInstance(service, key);
if (instance != null)
return instance;
throw new Exception("Could not locate any instances.");
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
protected override void PrepareViewFirst(Frame rootFrame)
{
navigationService = container.RegisterNavigationService(rootFrame);
}
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
Initialize();
var resumed = false;
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
resumed = navigationService.ResumeState();
}
if (!resumed)
DisplayRootView<MyTestView>();
}
}
}
CalMicSample\Helpers\ViewModelHelper.cs
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace CalMicSample.Helpers
{
public static class ViewModelHelper
{
public static bool Set<TProperty>(
this INotifyPropertyChangedEx This,
ref TProperty backingField,
TProperty newValue,
[CallerMemberName] string propertyName = null)
{
if (This == null)
throw new ArgumentNullException("This");
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentNullException("propertyName");
if (EqualityComparer<TProperty>.Default.Equals(backingField, newValue))
return false;
backingField = newValue;
This.NotifyOfPropertyChange(propertyName);
return true;
}
}
}
CalMicSample\Models\MonkeyMood.cs
namespace CalMicSample.Models
{
public class MonkeyMood
{
public string Message { get; set; }
public string ImagePath { get; set; }
}
}
CalMicSample\ViewModels\MyTestViewModel.cs
using Caliburn.Micro;
using CalMicSample.Helpers;
using CalMicSample.Models;
using System;
namespace CalMicSample.ViewModels
{
public class MyTestViewModel : ViewModelBase
{
private string food;
private MonkeyMood mood;
public MyTestViewModel(INavigationService navigationService)
: base(navigationService)
{
}
public string Food
{
get
{
return food;
}
set
{
this.Set(ref food, value);
}
}
public MonkeyMood Mood
{
get
{
return mood;
}
set
{
this.Set(ref mood, value);
}
}
public void FeedMonkey(string monkeyFood)
{
if (string.Compare(Food, "banana", StringComparison.CurrentCultureIgnoreCase) == 0)
{
Mood = new MonkeyMood();
Mood.Message = "Monkey is happy!";
Mood.ImagePath = #"D:\Tryouts\CaliburnMicroSample\CalMicSample\CalMicSample\Assets\monkey-happy.jpg";
}
else
{
Mood = new MonkeyMood();
Mood.Message = "Monkey is unhappy";
Mood.ImagePath = #"D:\Tryouts\CaliburnMicroSample\CalMicSample\CalMicSample\Assets\monkeysad.jpg";
}
}
public bool CanFeedMonkey(string monkeyFood)
{
return !string.IsNullOrWhiteSpace(monkeyFood);
}
}
}
CalMicSample\ViewModels\ViewModelBase.cs
using Caliburn.Micro;
namespace CalMicSample.ViewModels
{
public abstract class ViewModelBase : Screen
{
private readonly INavigationService navigationService;
protected ViewModelBase(INavigationService navigationService)
{
this.navigationService = navigationService;
}
public void GoBack()
{
navigationService.GoBack();
}
public bool CanGoBack
{
get
{
return navigationService.CanGoBack;
}
}
}
}
CalMicSample\Views\MyTestView.xaml
<Page
x:Class="CalMicSample.Views.MyTestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CalMicSample.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:caliburn="using:Caliburn.Micro"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="171*"/>
<RowDefinition Height="86*"/>
<RowDefinition Height="382*"/>
<RowDefinition Height="129*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="172*"/>
<ColumnDefinition Width="328*"/>
<ColumnDefinition Width="183*"/>
</Grid.ColumnDefinitions>
<Button x:Name="FeedMonkey" Content="Feed" caliburn:Message.Attach="FeedMonkey" Grid.Column="2" HorizontalAlignment="Left" Height="72" Margin="7,7,0,0" Grid.Row="1" VerticalAlignment="Top" Width="138" FontSize="36"/>
<TextBox x:Name="txtFood" Grid.Column="1" HorizontalAlignment="Left" Height="66" Margin="10,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Banana" VerticalAlignment="Top" Width="636" FontSize="36"/>
<TextBlock x:Name="lblFeedMonkey" Grid.Column="1" HorizontalAlignment="Left" Margin="10,119,0,5" TextWrapping="Wrap" Text="Give some food to the monkey" Width="636" FontSize="36" FontWeight="Bold"/>
<TextBlock x:Name="lblMonkeyMood" Grid.Column="1" HorizontalAlignment="Center" Margin="59,25,37,10" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Center" Height="94" Width="560" FontSize="72"/>
<Image x:Name="imgMonkey" Grid.Column="1" HorizontalAlignment="Left" Height="362" Margin="10,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="636"/>
</Grid>
</Page>
CalMicSample\Views\MyTestView.xaml.cs
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace CalMicSample.Views
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MyTestView : Page
{
public MyTestView()
{
this.InitializeComponent();
}
}
}
Your feedmonkey method in your vm wants a string parameter, but your not supplying one when attaching to your button. See here for more info: http://caliburnmicro.codeplex.com/wikipage?title=Cheat%20Sheet
Rob is correct, you need to match the method signature of your FeedMonkey(string monkeyFood), at the moment so `Caliburn.Micro' is unable to locate the correct method.
You'd want to use something like:
// You wouldn't want to use a hard coded string, but this is how you'd do it.
caliburn:Message.Attach="FeedMonkey('banana')"
// Or you can pass some of the special values linked in Rob's answer (the Caliburn docs).
// You'd probably want a sub property, depending on what it was you were passing.
caliburn:Message.Attach="FeedMonkey($dataContext)"
I have created a simple attached property that enables dragging an item around the screen.
1/ Here's how you would implement it on your element:
<Rectangle Fill="Green" local:MyExtension.CanMove="True" />
2/ This works like a charm. So does this:
// in resources
<x:Boolean x:Key="MyCanMove">true</x:Boolean>
<Rectangle Fill="Blue" local:MyExtension.CanMove="{StaticResource MyCanMove}" />
3/ But one syntax does not work. This fails:
<Rectangle Fill="Red" local:MyExtension.CanMove="{Binding Path=CanMove}" />
What's different? The only thing different is that it is binding the value into the attached property instead of setting it explicitly or through a static resource.
I'm missing something. But what is it?
Here's the full XAML:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<ToggleSwitch Header="Enable Dragging"
HorizontalAlignment="Center"
IsOn="{Binding CanMove, Mode=TwoWay}">
<ToggleSwitch.RenderTransform>
<TranslateTransform Y="-100" />
</ToggleSwitch.RenderTransform>
</ToggleSwitch>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel.Resources>
<Style TargetType="Rectangle">
<Setter Property="Height" Value="100" />
<Setter Property="Width" Value="100" />
</Style>
<x:Boolean x:Key="MyCanMove">true</x:Boolean>
</StackPanel.Resources>
<Rectangle Fill="Green" local:MyExtension.CanMove="True" />
<Rectangle Fill="Blue" local:MyExtension.CanMove="{StaticResource MyCanMove}" />
<Rectangle Fill="Red" local:MyExtension.CanMove="{Binding Path=CanMove}" />
</StackPanel>
</Grid>
And here's the full code-behind:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
bool m_CanMove = true;
public bool CanMove
{
get { return m_CanMove; }
set
{
m_CanMove = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("CanMove"));
}
}
}
public class MyExtension
{
// canmove aproperty
public static bool GetCanMove(DependencyObject obj)
{
return (bool)obj.GetValue(CanMoveProperty);
}
public static void SetCanMove(DependencyObject obj, bool value)
{
System.Diagnostics.Debug.WriteLine("SetCanMove");
obj.SetValue(CanMoveProperty, value);
var rectangle = obj as FrameworkElement;
rectangle.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
rectangle.ManipulationDelta -= rectangle_ManipulationDelta;
if (value)
rectangle.ManipulationDelta += rectangle_ManipulationDelta;
}
public static readonly DependencyProperty CanMoveProperty =
DependencyProperty.RegisterAttached("CanMove", typeof(bool), typeof(MyExtension), new PropertyMetadata(false));
// implementation
static void rectangle_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var rectangle = sender as FrameworkElement;
var canMove = System.Convert.ToBoolean(rectangle.GetValue(MyExtension.CanMoveProperty));
if (canMove)
{
var transform = rectangle.RenderTransform as CompositeTransform;
if (transform == null)
rectangle.RenderTransform = (transform = new CompositeTransform());
transform.TranslateX += e.Delta.Translation.X;
transform.TranslateY += e.Delta.Translation.Y;
}
}
}
I'll remind you that this attached property works fine in the first two syntaxes. As a result, I can't imagine the error is in the attached property. And, I read on a few forums where path= is necessary to bind to an attached property, so I included that (though it didn't make a difference). Changing the Mode (OneWay, TwoWay) doesn't make a difference. Binding with an ElementName didn't make a difference. I am wondering if this simply isn't enabled in Windows 8.0 WinRT. Can anyone else get this to work?
EDIT: Solution
The problem was that without a changed event handler setup, the binding doesn't raise a changed event. Here's the updated MyExtension code:
public class MyExtension
{
// canmove aproperty
public static bool GetCanMove(DependencyObject obj) { return (bool)obj.GetValue(CanMoveProperty); }
public static void SetCanMove(DependencyObject obj, bool value) { obj.SetValue(CanMoveProperty, value); }
public static readonly DependencyProperty CanMoveProperty =
DependencyProperty.RegisterAttached("CanMove", typeof(bool), typeof(MyExtension), new PropertyMetadata(false, OnCanMoveChanged));
// respond to change
private static void OnCanMoveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var rectangle = d as FrameworkElement;
rectangle.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
rectangle.ManipulationDelta -= rectangle_ManipulationDelta;
if ((bool)e.NewValue)
rectangle.ManipulationDelta += rectangle_ManipulationDelta;
}
// implementation
static void rectangle_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var rectangle = sender as FrameworkElement;
var canMove = System.Convert.ToBoolean(rectangle.GetValue(MyExtension.CanMoveProperty));
if (canMove)
{
var transform = rectangle.RenderTransform as CompositeTransform;
if (transform == null)
rectangle.RenderTransform = (transform = new CompositeTransform());
transform.TranslateX += e.Delta.Translation.X;
transform.TranslateY += e.Delta.Translation.Y;
}
}
}
This is just speculation as I don't have a compiler in front of me, but I'm wondering if the binding infrastructure doesn't use the exposed methods you created GetCanMove etc.
Try registering a property changed method in the PropertyMetadata
new PropertyMetadata(false, OnCanMoveChanged)
and have the setup and teardown code in there
private void OnCanMoveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
I'm using Microsoft Activity Library Designer; For some reasons I need to use ListBox to show some information in it.But I have a problem with it's ItemsSource binding.My Activity side property is like this:
private ObservableCollection<string> _selectedItems;
public ObservableCollection<string> SelectedItems
{
get
{
if (_selectedItems == null)
{
ObservableCollection<string> items = new ObservableCollection<string>();
return items;
}
return _selectedItems;
}
set
{
_selectedItems = value;
}
}
And my XAML side code is like this:
....
<Button Content="Add Item" HorizontalAlignment="Stretch" Grid.Column="0"
Click="Button_Click" Margin="5, 0, 5, 5"/>
<Button Content="Remove Item" HorizontalAlignment="Stretch" Grid.Column="1"
Click="DelButton_Click" Margin="5, 0, 5, 5"/>
....
<ListBox x:Name="LstSelectedPosts" MinHeight="20" ItemsSource="{Binding Path=ModelItem.Selecteditems, Mode=TwoWay}"/>
....
Now when I try to Add/Remove an item to/from this ListBox in Add Item and Remove Item buttons click event, debugger shows me an error that tells I can't modify the ListBox binding source.
So how can I change this Listbox's Items?
Ok there are some errors in your code that could cause the problem.
In the getter, I think you should have this.
if (_selectedItems == null)
{
_selectedItems = new ObservableCollection<string>();
}
return _selectedItems;
In your version, _selectedItems never get initialized.
In the Xaml code, when you set the ItemSource, you wrote Seleceteditems instead of SelectedItems this error doesn't cause an error when you compile but your listBox doesn't have its itemSource setted to the correct element.
And then, you didn't specify the source in
ItemsSource="{Binding Path=ModelItem.Selecteditems, Mode=TwoWay}
that means the source is by default, the DataContext of your object and that DataContext should be initialized with an object that has a public property named ModelItem which has in turn a public property named Selecteditems.
Hope it works.
Here is a small example.
in my xaml file
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="LstSelectedPosts" VerticalAlignment="Top" Width="294"
ItemsSource="{Binding Path=SelectedItems, Mode=TwoWay}"/>
<Button Content="Add Item" HorizontalAlignment="Stretch" Click="Button_Click" Margin="321,110,68,170"/>
<Button Content="Remove Item" HorizontalAlignment="Stretch" Click="DelButton_Click" Margin="321,147,68,129"/>
</Grid>
in my xaml.cs file
public partial class MainWindow : Window
{
private CDataContext _myCDataContext;
public MainWindow()
{
InitializeComponent();
_myCDataContext = new CDataContext();
DataContext = _myCDataContext;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_myCDataContext.Add();
}
private void DelButton_Click(object sender, RoutedEventArgs e)
{
_myCDataContext.Remove(LstSelectedPosts.SelectedItem.ToString());
}
}
and my CDataContext class
class CDataContext
{
private int _count = 0;
private ObservableCollection<string> _selectedItems;
public ObservableCollection<string> SelectedItems
{
get
{
if (_selectedItems == null)
{
_selectedItems = new ObservableCollection<string>();
}
return _selectedItems;
}
set
{
_selectedItems = value;
}
}
public void Remove(string s)
{
SelectedItems.Remove(s);
}
public void Add()
{
SelectedItems.Add(_count.ToString());
_count++;
}
}