Issue
I have button in Xaml and I have mentioned the method to be called in the click event as below.
<Button BorderRadius="1" BackgroundColor="#FFCA00" Text="Sign In" TextColor="Black" Clicked="login_clicked" HorizontalOptions="FillAndExpand" HeightRequest="44" WidthRequest="220" FontSize="19">
And the method in the backend C# as
public void login_clicked(object sender, EventArgs e)
{
try
{
ValidateUser();
}
catch (Exception ex){}
}
The problem is when I use Sdk Assemblies
in linking options this works perfectly, but when I use Sdk and User Assemblies
this method somewhat gets stripped and I get an exception at runtime saying no event login_clicked
found.
I know this is because of the linker but I don't know the reason behind this. Doesn't the linker parse the Xaml to check if this method is used or not(I mean to say if this is method is necessary and should not be stripped out).
When I register the event from C# as below the linker does not strip out the login_clicked method.
btnLogin.Clicked += login_clicked;
Solution
XAML
is not parsed as compile time, it is basically a resource string.
Two options:
Flag the method with a [Preserve] attribute so the linker does not remove it.
[Preserve]
Ref: Cross platform Linking Ref: iOS Linker Ref: Android Linker
Precompile your XAML into IL so the linker knows that the event is being used.
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
This has the added benefit of compile-time checking your XAML, some decrease in XAML loading and instantiation time, and slight memory reduction.
Ref: Using the XAML compiler to increase the performance of a Xamarin.Forms app
Answered By - SushiHangover
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.