I have a xamarin forms application and I want to add a footer to the flyout menu. After reading the official documentation it should be straightforward, just adding a few lines into the AppShell.xaml like so:
<Shell.FlyoutFooterTemplate>
<DataTemplate>
<Label HeightRequest="300" BackgroundColor="Red"/>
</DataTemplate>
</Shell.FlyoutFooterTemplate>
This works perfectly fine when I tried it in a new project, but for some reason, it doesn't work in my current application giving this error:
Error XLS0415 The attachable property 'FlyoutFooterTemplate' was not found in type 'Shell'.
I tried to find the definition of FlyoutHeaderTemplate and I found this in Shell [from metadata] file:
[1]: https://i.stack.imgur.com/xqZub.png
public Shell();
...
public DataTemplate FlyoutHeaderTemplate { get; set; }
public FlyoutHeaderBehavior FlyoutHeaderBehavior { get; set; }
public object FlyoutHeader { get; set; }
There should be a similar definition for both, Header and Footer, but there is only one for the Header. The file cannot be edited and I was not able to locate it either. Any ideas why the definition for Footer is missing, how can I add it, or workarounds?
PS: Adding the footer from C# code doesn't work either and I tried to clean/rebuild and resetting both, PC and VS.
First, Confirm that you can add a simple Header:
<Shell.FlyoutHeader>
<Label Text="This is the header." />
</Shell.FlyoutFooter>
If that doesn't work, then you are doing something fundamentally wrong - post the code for the <Shell> XAML, within which you added those lines. Make sure you include those lines, to show where in the XAML they are. Make sure they are between <Shell> and </Shell>, but not nested inside some deeper node. For example, they musn't be inside a <StackLayout> or <ContentView> or other container - they must be direct children of the <Shell>.
Unless you are doing something fancy, you don't need a Template.
Try simply:
<Shell.FlyoutFooter>
<Label HeightRequest="300" BackgroundColor="Red"/>
</Shell.FlyoutFooter>
If 1) above works, but not 2), then your project is referencing an out-of-date version of Shell. Fixes:
A. Check that project's Xamarin.Forms nuget doesn't need an Update. (Solution / Manage Nugets.)
B. OR delete bin and obj folders. Then Rebuild Solution.
C. Worst case, start with a new project, in which you are able to use that functionality, and add back in all your files.
Related
Xamarin team introduced 2 different way of stand-alone-resource-dictionaries. Obviously there is a big difference but they neglected to write in their documentation.
Basically if i use without cs code behind file and without x:class defined as defined in the documentation and referencing like that in my page
<ResourceDictionary Source="GradientStyles.xaml"/>
GradientStyles.xaml look like below and i am updating GradientStartColor , GradientStopColor of PancakeView on runtime based on the theme selected.
<pancakeView:GradientStopCollection x:Key="BackgroundGradientStops">
<pancakeView:GradientStop Color="{StaticResource GradientStartColor}"
Offset="0"/>
<pancakeView:GradientStop Color="{StaticResource GradientStopColor}"
Offset="0.74"/>
</pancakeView:GradientStopCollection>
Implementation above just not working. It doesn't update Colors at all.
The same exact code;
Works in ContentPage.Resources directly. It will update
Works if i define a stand-alone-resource-dictionary with cs code behind file and implement on the page like this below
<ResourceDictionary.MergedDictionaries>
<resDics:GradientStyles />
</ResourceDictionary.MergedDictionaries>
There is that annotation in the documentation but i don't get what exactly it means.
This syntax doesnot instantiate the MyResourceDictionary class.
Instead, it references the XAML file.
Does it mean that it extends the contentpage using like partial class or inherits. Or is it cached only once and remains static? And when you use it with code behind class, it creates a new instance every time?
Finally is that a bug or feature? :)
I've been searching for days now for a guide on how to create the custom playback controls for LibVLCSharp that everyone seems to talk about, which I never found a guid for.
I simply want to create other buttons with event handlers for the bottom playback control panel, I tried this but throws a System.NullReferenceException exception on startup while getting into break mode...
<vlc:MediaPlayerElement MediaPlayer="{Binding MediaPlayer}" LibVLC="{Binding LibVLC}">
<vlc:MediaPlayerElement.PlaybackControls>
<vlc:PlaybackControls>
<vlc:PlaybackControls.ControlTemplate>
<ControlTemplate>
<Grid>
<StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand">
<Button Grid.Column="0" Text="Test 1"/>
<Button Grid.Column="1" Text="Test 1"/>
<Button Grid.Column="2" Text="Test 1"/>
</StackLayout>
</Grid>
</ControlTemplate>
</vlc:PlaybackControls.ControlTemplate>
</vlc:PlaybackControls>
</vlc:MediaPlayerElement.PlaybackControls>
</vlc:MediaPlayerElement>
I want it to act just like the original one (Auto hides, overlays on tapping, etc...) but with my own layout and controls. I also thought about using the existing one and try to override their handler to implement my own code and override the text property for each button to change its icon but no luck of finding any help.
Thanks in advance ^_^
The code you are interested in is here: https://code.videolan.org/videolan/LibVLCSharp/-/blob/3.x/src/LibVLCSharp.Forms/Shared/Themes/Generic.xaml
I also thought about using the existing one and try to override their handler to implement my own code and override the text property for each button to change its icon
That'd be the way to go.
This previous SO question might answer your question: https://stackoverflow.com/a/14217500/4064749
Just create a new Style based on PlaybackControlsStyle, override what you want and then set it on the PlaybackControls element.
I created https://code.videolan.org/videolan/LibVLCSharp/-/issues/309 recently to track the need of a tutorial to customize the MediaElement.
Further docs on style inheritance: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/xaml/inheritance
I finally found the problem which was making exceptions, when I create custom control template which completely correct, the MediaPlayerElement code behind by LibVLCSharp developers itself cannot find the elements with the names defined anymore as they used hardcoded names for the buttons and views instead of using bindings and dynamic setters.
Thus, several workarounds could be made to fix such issue, here are some of my ideas:
Use the generic style documented here and modify it without removing any elements but rather hide them out or overlay them.
Create your own style with controls obtaining the same names of the original ones.
Find a way to modify or maybe create a whole new playback control element using the original one which can be found here and here.
Thanks to mfkl's answer which helped me find out how everything worked under the hood to come up with the explaination, even though this took me a couple of days to figure out.
I use prism on the project. My problem is: i have a View OrdemServicoView and a model OrdemServicoViewModel, OrdemServicoView has a syncfusion list... inside has a DataTemplate with a some propertys pass and i can bind, but when i try use command inside template it doesnt work, the command code is in OrdemServicoViewModel... how can i do that?
OrdemServicoViewModel:
public DelegateCommand<object> ItemSelectedCommand { get; set; }
OrdemServicoView (List):
<syncfusion:SfListView.ItemTemplate>
<DataTemplate>
<templates:OrdemServicoCardView />
</DataTemplate>
</syncfusion:SfListView.ItemTemplate>
In OrdemServicoCardView i try to make a button with ItemSelectedCommand binded
The problem you are having has to do with Binding Context. For instance normally within your View your BindingContext is your ViewModel. When working with a DataTemplate inside of something like a ListView your BindingContext is instead whatever data model you may be passing to that ListView as the ItemsSource. The trick therefore is to be able to reference back to the original ViewModel.
<syncfusion:SfListView.ItemTemplate x:Name="lv">
<DataTemplate>
<Button Text="Some Button"
Command="{Binding BindingContext.MyCommand,Source={x:Reference lv}}" />
</DataTemplate>
</syncfusion:SfListView.ItemTemplate>
How we do it is rather simple, first you need to provide a name on an element that has our ViewModel as it's Binding Context. You can pick anything you like really, I usually just reference the Parent Page but the actual List View will work just fine as well. Next we need to add that named element as the Binding Source. Finally we need to realize our new Binding Context is the Element we added as the Source, so we must prepend our Binding with the BindingContext property to have the proper scope into our ViewModel.
I want to display an Image in my Xamarin forms project using XAML but is is not displaying.
This is the code:
<Image x:Name="myimage" Source="logo.jpg"/>
I have set myimage's build action to embedded resource. I also tried this
<Image x:Name="myimage" Source="MyXamarinProject.logo.jpg"/>
Where MyXamarinProject is my namespace. But both not working. What is wrong here?
The recommended way to avoid decreasing application performance or problems displaying the images on different screen resolutions is adding each image on each platform specific folder:
Android: Project -> Resources -> Drawable (or each resolution drawable folder (-hdpi,-xhdpi...))
iOS: You can create an asset catalog for each image or use Project -> Resources folder.
And then, use the image name on XAML file. No code needed.
Upon inspecting the Microsoft Docs for this, it is not as easy as just specifying the path.
First, Create an IMarkupExtension
[ContentProperty (nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue (IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
return imageSource;
}
}
Then consume it in your XAML:
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
x:Class="WorkingWithImages.EmbeddedImagesXaml">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<!-- use a custom Markup Extension -->
<Image Source="{local:ImageResource WorkingWithImages.beach.jpg}" />
</StackLayout>
</ContentPage>
Note that you have to add a custom namespace to your page, in this case: xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
So, I can not display an image using XAML only. I am pretty satisfied with both the answers. Although this is how I displayed the image without pasting into the android and iOS(to their respective folders) and without making IMarkupExtension.
I put one line of code into C# code behind and that worked:
myimage.Source = ImageSource.FromResource("MyXamarinProject.logo.jpg");
//logo.jpg must be Embedded Resource.
This could not be the right answer but it can be a choice. Thanks!
I had the same problem on Visual Studio Mac:
my XAML :
<Image x:Name="myimage" Source="logo.jpg"/>
my resources was : logo.jpg
But the file was saved in the file system as Logo.jpg (Capital L).
I just modified it to logo.jpg and rebuild all.
I'm trying to do a simple localization of my app, making it support English (default) and Danish.
I've followed the MS tutorial, and I've looked at some samples but for some reason the simulator does not show the danish version when I choose danish language as the simulator language.
Here's what I've done:
Added supported culture: dk-DK;
Changed assembly info to use "English" as default.
Added the resource to app.xaml:
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:LåneRegnskab" x:Key="LocalizedStrings" />
</Application.Resources>
Added "AppResources.resx" and "AppResources.dk-DK.resx" to project with the strings.
To use the strings I write:
"{Binding Path=LocalizedResources.Title, Source={StaticResource LocalizedStrings}}"
LocalizedStrings class:
public class LocalizedStrings
{
public LocalizedStrings()
{
}
private static AppResources localizedResources = new AppResources();
public AppResources LocalizedResources { get { return localizedResources; } }
}
This all works for the english strings, but they do not change when I'm in danish mode. What am I missing here? :(
Nothing obvious wrong with your code. Try force-chance the culture in App.xaml.cs with the following code in the InitializePhoneApplication method.
private void InitializePhoneApplication()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
Update
Also ensure that SupportedCultures in your ProjectName.csproj file is set to support both languages, like this: <SupportedCultures>en;da-DK</SupportedCultures>
Thanks to Claus, I solved my problems (I seem to have made all the mistakes getting there) but here's all the settings that work for me.
I'm supporting English and Spanish and changing the region of the emulator to see it work.
In the .csproj
<SupportedCultures>en;es;</SupportedCultures> <-- I was being too specific on language here
I also had AppResources-es.resx <-- Rather than .es
In my GamePage.xaml
I made the mistake here of having LocalisedStrings in both source and Path.
In the App.xaml I didn't add the namespace inline, but the same otherwise.
Hopefully it's a mistake in one of these steps as it was in my case.
I struggled with the same problem and I've just found the solution. In the csproj file the node is defined by default, but I didn't noticed that and I created another one at the first lines...
So if you remove this(or set your cultures here) it will probably work.
<SilverlightApplication>true</SilverlightApplication>
<SupportedCultures>
</SupportedCultures>
<XapOutputs>true</XapOutputs>
You have to change the csproj file as in the example bellow
http://msdn.microsoft.com/en-us/library/dd941931%28v=vs.95%29.aspx