I need to cache the image, but it doesn't cache.
I have following converter :
public class ImageSourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
Uri uri = new Uri(UrlParser.GetRootURL() + "/Output/" + (int)value);
BitmapImage bitmapimage = new BitmapImage(uri)
{
CreateOptions = BitmapCreateOptions.DelayCreation | BitmapCreateOptions.BackgroundCreation
};
return this.bitmapimage;
}
}
XAML :
<Image Source="{Binding Id, Converter={StaticResource imageSourceConverter}}"
MaxHeight="150" MaxWidth="120" />
Any ideas on how cache them?
Related
I have a Image in my xaml in which the source is a URL that Iam binding.The URL from json will be like this : "/images/Uploads/e0111.png". I have the URL in my Common values stored class as CommonValues.URL. How can I add this "CommonValues.URL" before the json at the time of binding? So that the source for Image will be http://example.com//images/Uploads/e0111.png.?
If you need Uri
var myUrl= new Uri(CommonValues.URL + "images/Uploads/e0111.png");
If string than
var myUrl=CommonValues.URL + "images/Uploads/e0111.png";
Or you can do it like this in your ViewModel or Page
public string Url => string.Format("{0}{1}", CommonValues.URL,"/images/Uploads/e0111.png");
Then in XAML:
<Button Text="{Binding Url}"/>
You can use a converter, which will allow you to reuse in all your views/application
public class UrlConverter : IValueConverter
{
#region IValueConverter implementation
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var test = value as string;
if (!string.IsNullOrEmpty(test))
{
return CommonValues.URL + test;
}
return false;
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
#endregion
}
Then, in your page:
<ContentPage.Resources>
<ResourceDictionary>
<converter:UrlConverter x:Key="UrlConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<Image Source="{Binding YourProperty, Converter={StaticResource UrlConverter}}"/>
Apparantly in Xamarin forms there isn't an option to use FallbackValue or TargetNullValue, how could I use a converter to accomplish the task?
I'm looking to have visibility default to null if the data binding object is null.
NullConverter.cs
public class NullConverter : IMarkupExtension, IValueConverter
{
public object IsNullValue { get; set; }
public object IsNotNullValue { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? IsNullValue : IsNotNullValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
XAML
<Entry Text="{Binding WhateverProperty}" />
<Button IsVisible="{Binding WhateverProperty, Converter={local:NullConverter IsNullValue=False, IsNotNullValue=True}}" />
For my testing, WhateverProperty was a string that was originally set to null, whenever I update the Entry, the button shows up. Of course, you can use it with any type of property.
How to set credit card number format In xaml
Like
1234-1234-1234-1234
This can be easily achieved by using Value converters.
public class CreditCardNumberValueConverter : IValueConverter
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var builder = new StringBuilder(Regex.Replace(value.ToString(), #"\D", ""));
foreach (var i in Enumerable.Range(0, builder.Length / 4).Reverse())
builder.Insert(4*i + 4, " ");
return builder.ToString().Trim();
}
public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Regex.Replace(value.ToString(), #"\D", "");
}
}
After initialising this in Styles.cs, you can apply it to the Text property of the control in XAML as:
Text="{Binding CardNo, Converter={StaticResource CreditCardNumberValueConverter}}"
Likewise, Phone numbers too can also be formatted.
<Label Text="{Binding YourVmPropertyNameHere StringFormat='{0:0000-0000-0000-0000}'}">
This is assuming that your view model has already converted the number to an integer and set it to the property "YourVmPropertyNameHere".
Currently I am working on Xamarin.Forms and wondering about any possibility to add show/hide option to an entry field?
I have solved a similar issue by using an expand/collapse icon above a number of entry fields.
The show/hide element in XAML
Add a clickable image with fixed size(20x20) referring to embedded resources in the PCL:
<Image Source="{Binding ShowHideIcon, Converter={StaticResource StringToResImageSourceConverter}}" WidthRequest="20" HeightRequest="20"">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ShowHideCommand}" />
</Image.GestureRecognizers>
</Image>
The ViewModel processes the command:
Switch the boolean every time the image is touched.
public bool EntryVisible { get; set; }
public Command ShowHideCommand{
get {
return new Command((object o) => {
EntryVisible = !EntryVisible;
if (EntryVisible) {
ShowHideIcon = "ic_collapse";
} else {
ShowHideIcon = "ic_expand";
}
}
}
}
The label and Entry in XAML
Bind the IsVisible attribute of the Label and Entry to the boolean in the ViewModel.
<Label Text="Quantity" IsVisible="{Binding EntryVisible}" />
<Entry Text="{Binding Quantity}" IsVisible="{Binding EntryVisible}" />
For completeness sake, I have used https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images to store images ic_expand.png and ic_collapse.png in the PCL Resources folder.
A Converter is required to turn a string e.g. "ic_expand" into an image reference that XAML can use.
public class StringToResImageSourceConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var resString = (string)value;
if (!string.IsNullOrEmpty(resString)) {
return ImageSource.FromResource("ProjectName.Resources." + resString + ".png");
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
Entry entry = new Entry();
// Hide it
entry.IsVisible = false;
I have a collection of items in my app, and I want to set the Content of a ContentPresenter to one of these items. The item will be randomly defined by an int index. I can bind an item like this:
<ContentPresenter Content={Binding Items[0]}/>
but not like this:
<ContentPresenter Content={Binding Items[{Binding Index}]}/>
I've seen a number of answers suggesting using MultiBinding in WPF, but this isn't available in UWP. Is there an alternative?
You could create a view model property, returning Items[Index]:
public string RandomItem => Items[Index];
For the PropertyChanged notifications to work, you will need to raise the event whenever Index or Items changes, e.g.:
public int Index
{
get { return _index; }
set
{
_index = value;
RaisePropertyChanged();
RaisePropertyChanged(() => RandomItem);
}
}
If you prefer to have the logic in the view and go the multi-binding way, you can use the Cimbalino toolkit. For that to work, first add 2 NuGet packages:
Cimbalino.Toolkit
Microsoft.Xaml.Behaviors.Uwp.Managed
Now you can create a converter:
public class CollectionIndexConverter : MultiValueConverterBase
{
public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var collection = (IList) values[0];
var index = (int?) values[1];
return index.HasValue ? collection[index.Value] : null;
}
public override object[] ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
And use it from XAML:
<ContentPresenter>
<interactivity:Interaction.Behaviors>
<behaviors:MultiBindingBehavior PropertyName="Content" Converter="{StaticResource CollectionIndexConverter}">
<behaviors:MultiBindingItem Value="{Binding Items}" />
<behaviors:MultiBindingItem Value="{Binding Index}" />
</behaviors:MultiBindingBehavior>
</interactivity:Interaction.Behaviors>
</ContentPresenter>