Send a boolean as an Action parameter in Caliburn Micro - xaml

This is my XAML View (some code omitted for readability):<Window ... xmlns:c="http://www.caliburnproject.org">
<Button Content="Close without saving" c:Message.Attach="Close(false)" />
<Button Content="Save and Close" c:Message.Attach="Close(true)" />
</Window>
And here's the code in the ViewModel:
public void Close(bool save)
{
if (save)
{
// save the data
}
TryClose();
}
This doesn't work - of course - because the action parameters "true" and "false" aren't objects or object properties in the XAML. How can I make this work, and send a boolean as an Action parameter in Caliburn Micro?

If you put single quotes around the parameter name, it will properly convert for you.
<Button Content="Close without saving"
c:Message.Attach="Close('false')" />
<Button Content="Save and Close"
c:Message.Attach="Close('true')" />

You can try to use interactivity + triggers:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cl:ActionMessage MethodName="MyMethod" >
<cl:Parameter Value="True">
</cl:Parameter>
</cl:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>

Related

Xamarin Forms - adding the calling object as a parameter on a tap gesture recognizer

What is the correct syntax for adding the image in the XAML below as the parameter on the Command?
<ffimageloading:CachedImage Source="{Binding Source}" Aspect="AspectFit" CacheType="Memory" Opacity="2" x:Name="smallImage" >
<Image.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding Path=BindingContext.SetImageCommand, Source={x:Reference this}}"
CommandParameter="{Binding smallImage}" />
</Image.GestureRecognizers>
</ffimageloading:CachedImage>
And the command code it's bound to (CustomCachedImage is just a class derived from a cached image, with an imageName field added)
There will be multiple instances of the calling image as it is in a data template as part of an image slider, so I can't just get the control by name I have to make sure it is the calling control being passed.
public ICommand SetImageCommand
{
get
{
return new Command<CustomCachedImage>((_image) =>
{
string imgName = _image.ImageName;
SetImg(imgName);
});
}
}
changed "{Binding smallImage}" to "{Binding .}" and I got what I needed

Localize type arguments in XAML (e.g. x:String)

In Localizing Xamarin.Forms Apps with RESX Resource Files one can translate strings, images and so on. You can do this most of the time:
<Label Text="{i18n:Translate NotesLabel}" />
Now I want to use a Picker:
<Picker x:Name="IndustryTypePicker1" SelectedIndex="{Binding Industry}" HorizontalOptions="FillAndExpand">
<Picker.Items>
<x:String>1</x:String>
<x:String>2</x:String>
</Picker.Items>
</Picker>
Here we use <x:String> tags. How can these type arguments be localized in XAML? Or is this only possible in code?
I haven't tried, but this could work:
<Picker>
<Picker.Items>
<i18n:TranslateExtension Text="1" />
<i18n:TranslateExtension Text="2" />
</Picker.Items>
</Picker>
To get it to work with XamlC on, you might have to fix the signature of the TranslateExtension from:
public class TranslateExtension : IMarkupExtension
to:
public class TranslateExtension : IMarkupExtension<string>
and implement the new interface:
string IMarkupExtension<string>.ProvideValue(IServiceProvider serviceProvider)
{
...
return translation;
}
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
return (this as IMarkupExtension<string>).ProvideValue(serviceProvider);
}

How to change open animation of flyout menu

I have a menu flyout which is opened on ListView Items. The default open animation is quite slow is there a way to change the default animation of flyout menu?
EDIT
I am using following dependency property for showing context menu on list view item it works fine but when context menu is shown it squeezes the the whole view a bit. I do not want to squeeze the page when context menu is opened.
public class OpenMenuFlyoutAction:DependencyObject,IAction
{
public object Execute(object sender, object parameter)
{
if (!Global.IsDisabledShowContextMenuOnListView)
{
FrameworkElement senderElement = sender as FrameworkElement;
FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);
flyoutBase.ShowAt(senderElement);
}
return null;
}
}
List Item data template
<DataTemplate x:Key="MemberListItemDataTemplate">
<Grid Width="{Binding ElementName=searchView,Path=ActualWidth}" Background="{Binding ItemBackground}"
Margin="0,0,0,20" Height="auto">
<i:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="Holding">
<helpers:OpenMenuFlyoutAction />
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="share" RequestedTheme="Dark" Command="{Binding ElementName=pageMyProfile, Path=DataContext.ShareMemberDetails}" CommandParameter="{Binding item99}" />
<MenuFlyoutItem Text="reomve from members" RequestedTheme="Dark" Command="{Binding ElementName=pageMyProfile, Path=DataContext.RemoveMember}" CommandParameter="{Binding item99}" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
...
</DataTemplate>
Yeah, you need to create a new Style targeting MenuFlyoutPresenter in your resources
<Style TargetType="MenuFlyoutPresenter">
If you copy it from Program Files (x86)\Windows Phone Kits\8.1\Include\abi\Xaml\Design\generic.xaml you'll notice there are already some storyboards inside for various visual states that you need to change in order to get a different animation.
I wrote about something very similar in my blog post MenuFlyout flip animation on Windows Phone WinRT

Bind button header to variable + fix text in xaml

I want to bind a button header to a file name, but I want to have fix text before and after the filename. Is this possible in xaml without code behind?
Something like that:
<Button Header="Save {Binding ActiveDocument.FileName} as..." Command="{Binding ActiveDocument.SaveAsCommand}" />
Well I have never tried that to be honest. But Binding object has StringFormat property.
So you can simply try this.
<Button Content="{Binding ActiveDocument.FileName, StringFormat='Save {0} as...'}" Command="{Binding ActiveDocument.SaveAsCommand}" />
another possible way is setting Buttons ContentStringFormat property
<Button Content="{Binding ActiveDocument.FileName}" ContentStringFormat="Save {0} as..." Command="{Binding ActiveDocument.SaveAsCommand}" />

MVVM-Light EventToCommand Behavior for CheckBox Checked/Unchecked in Silverlight

I would like to handle the Checked and Unchecked events of a Checkbox control and execute a command in my ViewModel. I wired up an EventTrigger for both the Checked and Unchecked events as follows:
<CheckBox x:Name="chkIsExtendedHr" IsChecked="{Binding Schedule.Is24Hour, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<GalaSoft_MvvmLight_Command:EventToCommand
CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}"
Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<GalaSoft_MvvmLight_Command:EventToCommand
CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}"
Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
I defined a RelayCommand in my ViewModel and wired up an action for it:
public RelayCommand<Boolean> SetCloseTime{ get; private set; }
...
SetCloseTime= new RelayCommand<bool>(ExecuteSetCloseTime);
The parameter in the action for the command always resolves to the previous state of the CheckBox, e.g. false when the CheckBox is checked, and true when the CheckBox is unchecked.
void ExecuteSetCloseTime(bool isChecked)
{
if (isChecked)
{
// do something
}
}
Is this expected behavior?
I have a workaround where I have separate triggers (and commands) for the Checked and Unchecked and use a RelayCommand instead of RelayCommand<bool>. Each command executes correctly when the CheckBox is checked and unchecked. Feels a little dirty though - even dirtier than having UI code in my ViewModel :)
Thanks
I think using "Click" event instead of "Checked" or "UnChecked" can solve this problem with just one command and no additional code.
In XAML it will look like,
<i:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}" Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
now rest of you code should work the you wanted,
thanks,
Why don't you handle your actions in your Schedule.Is24Hour. In setter you always can see when that property is changed.
i do this for checking checkbox
for view
<CheckBox Margin="126,0,0,0" IsChecked="{Binding UseNOCODE, Mode=TwoWay}" Content="Reply Messages ?" />
for the modelview
private bool _useNOCODE = false;
public bool UseNOCODE
{
get
{
return _useNOCODE;
}
set
{
if (_useNOCODE == value)
{
return;
}
_useNOCODE = value;
RaisePropertyChanged("UseNOCODE");
UseNoCodeChecked();
}
}
private void UseNoCodeChecked()
{//check the properties and what you like}