Showing posts with label xamarin. Show all posts
Showing posts with label xamarin. Show all posts

Sunday, October 22, 2017

Xamarin Forms - Passing parameters to android/ios application from another/url

This post includes a sample on how to use incoming parameters in Xamarin forms app.

How to pass parameters to android application (Forms app)

1. Create a Xamarin forms project.
2. In you Xamarin.Forms PCL project > App.xaml.cs create a new method that accepts either a class object or individual params (ConfigureParams()).
3. In this method, use the values to perform custom logic or navigate to page.
4. Go to platform specific project.
In MainActivity.cs file, use the following code to retrieve params from Intent Extras.
Example Code :

var page = Intent.GetStringExtra("page") ?? "MainPage";


5. Use app object to call (ConfigureParams()) and pass values retrieved via Intent.GetExtra methods.

How to pass paramters to ios application (Forms app)

Create a method in app.xaml.cs as mentioned above.
1. In platform specific ios project,  right click Components> Get Components and install Rivets component.
2. Override OpenUrl method in AppDelegate.cs
3. Save instance of App object created in FinishedLaunching() as a private variable (_app). In OpenUrl use the following code to retrive values from url.

Example code:

// url is NSUrl param which is passed to OpenUrl
var rurl = new Rivets.AppLinkUrl(url.ToString()):
var page ="";
if (rurl.InputQueryParameters.ContainsKey("page"))
       page= rurl.InputQueryParameters["page"];
_app.ConfigureParams(/* params*/);
return true;

4. Open info.plist and go to Advanced tab.  Configure a Url Type with required Identifier a UrlScheme. This will allow app to recognize different urls schemes.
5. You can test application by running in simulator and open the url with configured scheme in safari.

Friday, March 17, 2017

[Code Sample] Image From Resource - IMarkupExtension

This extension allows to set image in XAML from resource images.

1. Extension code
   [ContentProperty("Source")]
    public class ImageResourceExtension : IMarkupExtension
    {
        public string Source { get; set; }
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            if (Source == null)
            {
                return null;
            }
            var imageSource = ImageSource.FromResource("MobileApp.Images." + Source);
            return imageSource;
        }
    }
2. Add namespace
 xmlns:resources="clr-namespace:MobileApp.Resources;assembly=MobileApp" 
3. Usage
 <Image Source="{resources:ImageResource  image.png}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="Fill"  />
    

Xamarin (Android) Title Bar Background Color (Popup Modal and Default Title Bar)

Setting BarBackgroundColor = Color.FromHex() will set the background color for the navigation page correctly however as soon as a page is pushed or a modal is opened that modal will not have the same color applied as above.

In order to change this popup modal color follow these steps:

1. Add Styles.xml (set as AndroidResource) in Resources\values folder in your android project.
2. Create a theme style and specify title background color. See xml below.

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>
    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">#003A63</item>
    </style>
</resources>

3. Set android application theme.
[Activity(Label = "My Label", Icon = "@drawable/icon", MainLauncher = true,
        Theme = "@style/CustomActionBarTheme",
        )]

Tuesday, March 14, 2017

Xamarin Version Matching Issue with XLabs and Designer Issues

Solution 1. Working on a Xamarin project I found out that if Xamarin Version doesn't exactly match with XLabs installed in the projects it can create hundreds of designer errors. This can be solved by ensuring that XLabs's Major.Minor.Build match with Xamarin libraries. Revision doesn't matter.

Solution 2. If you still face issues after matching, it may be that XLab team has not yet updated/compiled the package using latest xamarin and you can lookup/create a polyfill for missing resources.

Solution 3. Ensure that all packages are up to date and unnecessary old android sdks are not installed in Android SDK manager.

Worked for me after these changes above.

More to be discovered on this. Feel free to post any issues you may face after applying those fixes above.