Can you add xaml into your FlipTileData BackContent? - c#

Is it possible to set xaml inside the FlipTileData.BackContent? Basically I just want to be able to set a full layout with multiple lines, control over spacing and layout and maybe an inline image.
Is this possible or is the FlipTileData.BackContent limited to text only?
This is my current code for BackContent but I can not get anything other than just text to work in the there.
private FlipTileData GetSecondaryTileData()
{
FlipTileData tileData = new FlipTileData
{
Title = resortforTile.ListName,
BackgroundImage = new Uri("/Assets/Tiles/MountainTile.png", UriKind.Relative),
BackContent = ""
};
return tileData;
}

You cannot set XAML as BackContent but you can render the XAML into an image and set the BackBackgroundImage to this image.
More info at http://www.jayway.com/2012/04/03/advanced-transparent-live-tiles-with-count-for-windows-phone/

Related

how to cast (or convert) an ImageSource to FileImageSource [duplicate]

I am trying add a background image using the image property in button. The issue I'm facing is that i can't set StreamImageSource as button background. I encountered the error given below if I try to do so.
The Code I use to set Image:
ImageSource iconsource =ImageSource.FromStream(() => new MemoryStream(ImgASBytes));
Button Icon = new Button ();
Icon.Image = iconsource ;
The Error I encounter:
Error CS0266: Cannot implicitly convert type 'Xamarin.Forms.ImageSource' to 'Xamarin.Forms.FileImageSource'. An explicit conversion exists (are you missing a cast?)
ImageSource.FromStream () returns a StreamImageSource (see docs). Button.Image accepts only FileImageSource (see docs).
It means that what you're trying to achieve won't work, no matter how hard you try to cast one into the other.
Button.Image will accept images stored as resources in your platform projects, and loaded either with:
Icon.Image = ImageSource.FromFile ("foobar.png");
or
Icon.Image = "foobar.png";
The accepted answer is true that you can't cast StreamImageSource to FileImageSource, I think that the real question is about how to share images in a PCL and use them on a button, just like one would when creating an Image forms control.
The answer is to have a Grid which contains both a Button and an Image object, where the Image overlaps the Button.
For example, the C# code might look like this:
ImageSource imageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
Button iconButton = new Button ();
iconButton.VerticalOptions = LayoutOptions.FillAndExpand;
iconButton.HorizontalOptions = LayoutOptions.FillAndExpand;
var image = new Image();
image.Source = imageSource;
// So it doesn't eat up clicks that should go to the button:
image.InputTransparent = true;
// Give it a margin so it doesn't extend to the edge of the grid
image.Margin = new Thickness(10);
var grid = new Grid();
// If we don't set a width request, it may stretch horizontally in a stack
grid.WidthRequest = 48;
// Add the button first, so it is under the image...
grid.Children.Add(iconButton);
// ...then add the image
grid.Children.Add(image);
You may have to play with the sizes and thickness values but this should get you a clickable button with an icon.
As of Xamarin.Forms 3.4.0 you can now use ImageButton. You can use embedded images by using an extension method explained in this MS document
Careful with upper- and lowercase in filenames.
I was wondering, why my button-images were shown properly on the simulator, but not on my iPhone.
On the device the filename must match exactly, the simulator doesn't care about upper- and lowercase in filenames.
I use this and it works
var imageA = new Image();
imageA.Source=(FileImageSource)ImageSource.FromFile(allergeneLocation)};
or
var imageA = new Image()
{
BackgroundColor = Color.Teal,
Source = (FileImageSource)ImageSource.FromFile(allergeneLocation)},
};
Here is what I tried:
Button refreshBut = new Button
{
Image = (FileImageSource)
(ImageSource.FromFile("refreshBut.png"))
};
While it compiles I then get an unhandled null reference exception with the description: Object reference not set to an instance of an object. I am not sure if this will help anyone else try to solve this but I am at the same wall.

How to show more that one image in the radtreeview item (wpf - telerik )

I am adding image to the radtreeviewitem from resources programatically using the below code.
"/myAssembley;component/Resources/image1.png"
and the image is displaying successfully. Now i need to add another image which needs to be displayed next to the first image in the radtreeviewitem.
how to achieve it.?
Like the below image i need my treeviewitem to display a folder icon and a red square icon in a single treeview item.
If you do not have data binding and you are using RadTreeViewItems directly you can add the additional image in the Header of the item. For example:
var stackPanel = new StackPanel() { Orientation = System.Windows.Controls.Orientation.Horizontal };
var image1 = new Image() { Source = image1Path };
var image2 = new Image() { Source = image2Path };
var textBlock = new TextBlock() { Text = itemHeader };
stackPanel.Children.Add(image1);
stackPanel.Children.Add(image2);
stackPanel.Children.Add(textBlock);
var treeViewItem = new RadTreeViewItem()
{
Header = stackPanel,
};
It Works.
The proper way would be to create a DataTemplate with a grid or horizontal stackpanel. Put two images inside and in your model two Image Sources that you can bind too. Telerik doesn't have the best track record using the MVVM pattern, but the TreeView control is pretty decent with binding. If you need help with the model and the datatemplate, post some of your code here and we can work on it.

Set Adaptive Tile Background from Image

I am creating a TileNotification to display the last edited project on my app's tile. When doing this I want to set the tile's background to be the project cover image. I'm creating the TileContent like this:
TileContent content = new TileContent()
{
Visual = new TileVisual()
{
TileMedium = new TileBinding()
{
Content = new TileBindingContentAdaptive()
{
BackgroundImage = new TileBackgroundImage()
{
Source = new TileImageSource("ms-appx:///Assets/Images/MainPageBackground.jpg"),
Overlay = 10
},
Children =
{
new TileText()
{
Text = DataModel.Instance.CurrentProject.Title,
Align = TileTextAlign.Left
}
}
}
}
}
};
The problem is that the only way to set the Source property seems to be with a TileImageSource, which only accepts a string. And since the project's cover image is stored in ApplicationData.Current.LocalFolder... I can't just give it a string. Is there any way to set the image source from an actual image rather than a string?
After some more searching I found this method of doing it.
You can use the "ms-appdata:///Local" prefix to get the file. In my case:
$"ms-appdata:///local/Projects/{ProjectName}/Slides/0.gif".
That can then be handed in as the source.

Render CheckBox to Image for Tile

I want to display one or more CheckBoxes on a tile in my Windows Phone app. This works already for TextBlocks, but with a CheckBox it shows only the Text of the CheckBox and not the Checkmark itself.
This is a sample of my code:
public void CreateTile()
{
StackPanel panel = new StackPanel();
panel.VerticalAlignment = VerticalAlignment.Top;
panel.Margin = new Thickness(7.0, 7.0, 7.0, 0);
panel.Width = 336;
panel.Height = 336;
panel.Orientation = Orientation.Vertical;
// Create and add a CheckBox for each task
foreach (var task in _tasks)
{
TextBlock textBlock = new TextBlock();
textBlock.TextWrapping = TextWrapping.Wrap;
textBlock.Style = App.Current.Resources["PhoneTextLargeStyle"] as Style;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.Text = task.Text;
CheckBox checkBox = new CheckBox();
checkBox.IsChecked = task.IsDone;
checkBox.Content = textBlock;
panel.Children.Add(checkBox);
}
Grid layoutRoot = new Grid();
layoutRoot.Background = new SolidColorBrush(Colors.Blue);
layoutRoot.Width = 336;
layoutRoot.Height = 336;
layoutRoot.Children.Add(panel);
layoutRoot.Measure(new Size(336, 336));
layoutRoot.Arrange(new Rect(0, 0, 336, 336));
layoutRoot.UpdateLayout();
// Render grid into bitmap
WriteableBitmap bitmap = new WriteableBitmap(336, 336);
bitmap.Render(layoutRoot, null);
bitmap.Invalidate();
// Save background image for tile to isolated storage
Uri backgroundImage = TileHelper.SaveTileImage(bitmap);
}
If I create a tile with a background image created by the method above, the tile will look like this:
As you can see the text is displayed but there is no checkmark/square before the text.
I personally like to use Segoe UI Symbol as the Font Family in such situations. This gives me the flexibility to use Text and Symbols together while not messing around too much with code / images. SUS has great modern icons (or characters if you may call them) that are very much Metroish, I'd say.
Just open up Charmap (Win + R and type in charmap) and in the Font Select -> Segoe UI Symbol. Now you can select any character you like and paste into Visual Studio Editor itself. Yes, it works!
The symbol may not display properly in the Editor itself but it will at Runtime
Here are some suggestions:
Here are the corresponding characters:
☑

✅

Don't worry about them not looking right HERE. They should when you follow the above steps.
You can always "hack" it by using images of checkbox controls. Did you try to show created control in UI? i.e. adding it to page? Just to see if your code is executed correctly.
Or another solution would be to use check mark character - http://www.fileformat.info/info/unicode/char/2713/index.htm
I'll try to replicate this problem in my test app since it is strange that it does not work.

Adding image to a button in WPF C#, image not appearing

I want to create a button in my windows 8 desktop app, which will have an image and a text block. I have to do this using C# coding.
My code is as follows,
Button btn = new Button();
StackPanel btnContentPanel = new StackPanel();
btnContentPanel.Orientation = Orientation.Horizontal;
Image img = new Image();
img.Source = new BitmapImage(newUri(#"C:\Users\Desktop\Images\download.jpg"));
img.Stretch = Stretch.Uniform;
btnContentPanel.Children.Add(img);
TextBlock txBlock = new TextBlock();
txBlock.Text = "My Button";
btnContentPanel.Children.Add(txBlock);
btn.Content = btnContentPanel;
This is not giving any error but the image is not getting displayed. If I add another text block in place of the image, then its appearing, but not the image.
Am I missing anything ? Please help, thank you.
Try building your button like this:
Button btn= new Button
{
Width = 30,
Height = 30,
Content = new Image
{
Source = new BitmapImage(#"C:\Users\Desktop\Images\download.jpg"))
}
};
In the case of a 'missing' image there are several things to consider:
When Xaml can't locate a resource it might ignore it (when it won't throw a XamlParseException)
The Resource must be properly added and defined:
make sure it exists in your project where expected.
Make sure it is built with your project as a Resource.
(Right click -> Properties -> BuildAction='Resource')
Another thing to try in similar cases, which is also useful for reusing of the image (or any other resource):
Define your Image as a Resource in your Xaml:
<UserCondrol.Resources>
<Image x:Key="MyImage" Source.../>
</UserControl.Resources>
And later use it in your desired control/controls:
<Button Content={StaticResource MyImage} />

Categories

Resources