I have added the below line of code in App.xaml.cs for enabling xaml compilation in all the pages.
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace MyApp
{
}
Also I have added this line of code in my one of the xaml views to enable ZIndex in a specific control for Android.
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:VisualElement.Elevation="10"
So my doubt is, Is it affect the Xaml compilation? If I add the above line of code is it affect my View's Performance?
Note: I am assuming you are using a .NetStd library project and not a Shared Code project as the "container
" of your XAML code and that you are also using the latest version of Xamarin.Forms
android:VisualElement.Elevation="10"
VisualElement.Elevation will become a Forms-based bindable property attached to each control that you are assigning it in when compiled into IL via XAMLC, i.e.
IL_0085: ldsfld [Xamarin.Forms.Core]Xamarin.Forms.BindableProperty [Xamarin.Forms.Core]Xamarin.Forms.PlatformConfiguration.AndroidSpecific.VisualElement::ElevationProperty
So my doubt is, Is it affect the Xaml compilation? If I add the above line of code is it affect my View's Performance?
So it is compiled into IL, the XAML is not embedded in the assembly and of course is present on all platforms that this cross-platform NetStd/PCL assembly is deployed (that is why a platform-based bound property is used). At runtime the property is assigned and yes there is an "overhead" associated to do that:
IL_008a: ldc.r4 10
IL_008f: newobj instance void [netstandard]System.Nullable`1<float32>::.ctor(!0)
IL_0094: box [netstandard]System.Nullable`1<float32>
IL_0099: callvirt instance void [Xamarin.Forms.Core]Xamarin.Forms.BindableObject::SetValue([Xamarin.Forms.Core]Xamarin.Forms.BindableProperty,object)
Note: This "overhead" is on all platforms, it would just be on Android that setting the "native" field's Elevation would actually do anything.
Would it be faster and more memory efficient to directly assign the Elevation in a custom Android renderer, yes, but only you can determine if the (small) gain is worth it to your code base.
Related
An exception is thrown when setting "ItemContainerStyle" to "StaticResource TreeViewItemStyle" which is defined in generic.xaml. The message indicates the style is using "Windows.UI.Xaml.Controls.TreeViewItem" rather than "Microsoft.UI.Xaml.Controls.TreeViewItem".
I removed ItemContainerStyle assignment to confirm that it is the line causing the exception.
<ControlTemplate TargetType="muxcontrols:TreeView">
<muxcontrols:TreeViewList x:Name="ListControl"
ItemTemplate="{StaticResource TreeViewItemDataTemplate}"
ItemContainerStyle="{StaticResource TreeViewItemStyle}">
The problem seems obvious, but a proper solution evades me. To leverage the style, do I start prepending "muxcontrols:" in the generic.xaml file? This seems like a bad idea. Do I need to recreate the style and behaviors separately?
I tried (min build 17763 and target build 17134) to eliminate version problems, but it seems I need 17763 for both. Even though, I am not setting ItemsSource. I'm trying the technique used in the docs: "learn.microsoft.com/en-us/windows/uwp/design/…" for the Music Library TreeView sample. My real project's min version is 16299.
So, the issue was clear. The TreeView control is introduced from build 17134. If you're using the platform APIs, you need to make sure that your project's target version is 17134 or higher. Since you said you want to set ItemsSource. ItemsSource and its related APIs require Windows 10, version 1809 (SDK 17763) or later, so you need to make your project's target version is 17763.
Then, you could directly use the TreeView control like the following:
<TreeView></TreeView>
If you're using Windows UI Library APIs, you need to follow the Getting started with the Windows UI Library
document to download and install the Windows UI Library. Please note:
Important: To use WinUI 2.1, your project’s Min version must be 14393 or higher and the Target version must be 17763 or higher.
Then, if you want to use the TreeView control in WinUI library, in your XAML page, add a reference at the top of your page
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
On the XAML page, you could directly input the following:
<controls:TreeView></controls:TreeView>
I am using Xamarin.Forms, shared project template.
Here, I add controls to the content page such as a Label through say framework provided StackLayout.
Now in target apps - say for iOS/Andriod, I just need to set some text for this label. I have some platform specific code where I want to change the text value for the Label created in Shared Project.
How to do that?
I understand, Custom Renderers could be used. Is there any straight forward way that I am missing here?
You can, of course, use custom renderers... but imho it would be an overkill.
I'd use OnPlatform method - something like:
myLabel.Text = Device.OnPlatform<string>("text for iOS", "text for Android", "text for Windows Phone");
You can also do the same from XAML if you wish, like shown in this article (search "OnPlatform" there):
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/essential_xaml_syntax/
If the text you need is dynamic and you need to set it at run time from platform-specific project, than you need to assign your Label to some variable in the shared project and then reference this variable from platform projects.
I am converting my C# windows forms application to a C++\CLI windows forms application.
My C# version had a circular dependency between forms. For example, the 'Main' form creates 'Form2' and shows it, and when Form2 is closed it re-shows the hidden 'Main' form by accessing the static Main form instance through Main.activeWindow (in C++ this would require a circular dependency between headers and classes). There is also much more communication between the two forms - ie. 'Form2' changing a button color in form 'Main', or updating a property.
How can I break this dependency (I just don't want to have to mess around with forward declarations and such), but still having the same functionality?
Thanks for your help,
Alex
The only way is too split the class definition into the normal .h/.cpp files. Include the declarations in the .h and the bodies in the .cpp where they can see both class definitions.
I used ToggleSwitch control in windowsphone, It works fine in windowsphone7 version, takes Microsoft.Phone.Controls as a assembly reference. But in the windowsphone7.5 it asks assembly reference for ToggleSwitch control, it's not included in Microsoft.Phone.Controls. Anyone know the assembly reference for windowsphone7.5 ToggleSwitch control.
The ToggleSwitch control is part of the Silverlight Toolkit, and is part of the namespace Microsoft.Phone.Controls.
To enable it in your project, add a reference to the toolkit dll (or better yet, use NuGet to install it), and then reference the namespace in your XAML
(I'm sure you already know this stuff, and that the actual alias you choose isn't important as long as you use the same one when adding the control to the XAML - using "toolkit" here because that's what I normally do)
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
and make sure your declaration of the control in the XAML includes the namespace reference...
<toolkit:ToggleSwitch ... ><!-- more stuff here --></toolkit:ToggleSwitch>
I have made a custom control (round button with a fixed image) and tested that it works. However, I would like to reuse this particular control in other projects and hence thought of making a class library out of it. However, when I try to get the custom control to show in my other applications, the icons does not show even though the button responds to clicks.
I have tried to build the icon in the class library projects as Content and Resources and test but to no avail. (of course, I change the addressing of the icon in the code when I updated its build icon). At the moment, i have decided to leave the icon /icons/myimage.png to be built as Content. And, in the class library code (XAML), i am accessing it as "/icons/myimage.png".
So, would anyone have an idea on how I could get the round button to render properly in my projects? How should I build the class library project?
I would't make a graphics part of my class library, because most likely in the future you will need to customize it, so what I suggest is to make the following property in your custom module and set the image where you use it:
public ImageSource ButtonImage {get { return <button image>; } set { <set button image>; } }
I experimented a bit and found the solution. The idea is to use embedded resource to store the image in the dll. And then for loading it by the CustomControl, one can use constructs like this:
BitmapImage img = new BitmapImage();
img.SetSource(Assembly.GetExecutingAssembly().GetManifestResourceStream("MyLib.icons.my_icon.png"));
MyLib here is the name of the assembly for the class library. icons is the folder where the resource is kept in the assembly.