Xamarin: Open page from string - c#

I'm just starting to learn programming with Xamarin and C# and I really struggle with this question.
I have a master detail app. In my master page I would like to easily add buttons that link to a detail page.
To do that I would like to make a method that I can call to with two parameters:
Button text and the detail page linked to the button.
so basicly:
public class MainLink : Button
{
public MainLink(string name,string page)
{
Text = name;
Command = new Command(o => {
App.MasterDetailPage.Detail = new NavigationPage(new page);
App.MasterDetailPage.IsPresented = false;
});
}
}
This is what I have so far. But the line:
App.MasterDetailPage.Detail = new NavigationPage(new page);
ofcourse doesn't work.
How can I convert a string to a page type when I know the page's name already?

You can also use the awsome Activator.CreateInstance() which takes in a Type which would be your page.
In order to get from a string to a Type you could do one of two things. Either try to get the type using the string alone (which I have not actually tried in Xamarin Forms) like so:
Type somethingPageType = Type.GetType("SomethingPage");
Or you could just pass the page Type into your Button's constructor and use that like so:
....
public MainLink(string name,string page, Type pageType) {
....
}
So to put it all together, either:
public class MainLink : Button
{
public MainLink(string name,string page)
{
Text = name;
Command = new Command(o => {
Type pageType = Type.GetType(page);
App.MasterDetailPage.Detail = new NavigationPage(Activator.CreateInstance(pageType) as Page);
App.MasterDetailPage.IsPresented = false;
});
}
}
Or:
public class MainLink : Button
{
public MainLink(string name, Type pageType)
{
Text = name;
Command = new Command(o => {
App.MasterDetailPage.Detail = new NavigationPage(Activator.CreateInstance(pageType) as Page);
App.MasterDetailPage.IsPresented = false;
});
}
}
Two things to keep in mind though. First would be that if your page takes parameters into the page's constructor, you will need to use an overload of Activator.CreateInstance() to make that work.
Also, if you use the Xamarin Linker to remove unused stuff, it will probably mess this method up so you need to specifically tell the linker to leave your reflection code alone, for iOS and for Android
We do not link all code, only SDK Assemblies so we do not need to worry about the linker issue but it looks like you can use the following code on iOS to fix it (though I have not actually tested the code below):
Create a .cs file in your iOS project named whatever (LinkerIncludes.cs for example) and add the following code:
public class LinkerIncludes {
public void Include() {
var x = new System.ComponentModel.ReferenceConverter (typeof(void));
}
}

NavigationPage gets an instance of a page.
You can use a switch statement to initialize a new page, according to the page string:
switch (page)
{
case "page1":
App.MasterDetailPage.Detail = new NavigationPage(new Page1);
break;
case "page2":
App.MasterDetailPage.Detail = new NavigationPage(new Page2);
break;
default:
App.MasterDetailPage.Detail = new NavigationPage(new DefaultPage);
break;
}

Related

How do I inherit controls in other form in C# WPF?

I'm converting our old program written in VB6 to C# WPF, and there is this code that I'm having a hard time trying to convert.
In our old VB6 project. The code is written like this:
Select Case paymentType
Case "CASH":
Set dlgpayment = New dlgPaymentCash
dlgPaymentCash.lblChange = format(-gvAmountDue - gvAddOnAmount, "0.00") '/NJ-2011/10/25 Added gvAddOnAmount
Case "CARD":
If PaymentTypeSupports(gvPaymentType, "multiple") And PaymentTypeSupports(gvPaymentType, "remember") And TypeName(dlgpayment) = "dlgPaymentCreditCard" Then
Else
Set dlgpayment = New dlgPaymentCreditCard
End If
dlgPayment is declared as:
Window dlgPayment = new Window();
so whenever I want to inherit other windows, I will just simply code like this:
Set dlgPayment = new dlgPaymentCash //dlgPaymentCash is a form.
And whenever I do this, I was able to access the controls in the form dlgPaymentCreditCard like dlgPaymentCash.lblChange, but it doesn't work in C#.
This is roughly the conversion:
switch (paymentType) {
case "CASH":
var dlgpayment = new dlgPaymentCash();
dlgPaymentCash.lblChange = string.Format("{0:0.00}", -gvAmountDue - gvAddOnAmount);
//NJ-2011/10/25 Added gvAddOnAmount
break;
case "CARD":
if (PaymentTypeSupports(gvPaymentType, "multiple")
&& PaymentTypeSupports(gvPaymentType, "remember")
&& typeof(dlgpayment) == "dlgPaymentCreditCard") {
//.....
} else {
var dlgpayment = new dlgPaymentCreditCard();
}
break;
}
If the format() function is the standard VB6 one, you can either use the string.Format() function, or simply the .ToString() function.
You can access a variable inside a Window by callling WindowName.VariableName like this:
switch(paymentType)
{
case "CASH":
Window dlgPayment = new Window();
//Access variable lblChange in dlgPayment
dlgPayment.lblChange="something";
break;
case "CARD":
break;
default:
break;
}
Given that you create a class which inherited the Window class (Which represent the form) :
in xaml.cs
public partial class PaymentDialog : Window //inherit Window
{
public string lblChange {get;set;} //this should be a textblock/label in xaml, but i just assume it is a string.
}
You need to create instance of PaymentDialog by:
PaymentDialog dlgPayment = new PaymentDialog(); //notice the different
dlgPayment.lblChange = "XXX"; //you now can access it
Window dlgPaymentWindow = new Window();
dlgPaymentWindow.lblChange = "XXX";//you cannot access it since
//lblChange is not exist in WPF Window class

How to call a variable from .droid to .shared project in VS Xamarin?

this may be a very basic question but I'd like to ask if how can I output the variable from a .droid to a .shared project in VS Xamarin. Let's say I have a variable with value in a renderer from a .droid, then I'd like to call it from a .shared page. How can I do this?
ScannerPageRenderer.cs (From .droid)
try
{
var BarcodeText = result.text.ToString();
var BarcodeType = result.typeText.ToString();
}
If you have a ScannerPageRenderer then this means that you are using a custom Renderer for a Xamarin.Forms page.
To pass back the values you can either create public properties in the Page, for this example let's call it ScannerPage then pass the values in the Element object of the custom renderer as this is the Page you are attaching the custom renderer.
ex
private void Scan()
{
//or however you are getting the result...
var result = getScanResult();
var scanPage = Element as ScannerPage;
if (scanPage == null)
{
return;
}
scanPage.BarcodeText = result.text.ToString();
scanPage.BarcodeType = result.typeText.ToString();
}
Another way would be that instead of the two properties you create a public method where you pass in the result object the same way as above with the properties. In such method you will then assign your properties or fields with the values.
private void Scan()
{
var result = getScanResult();
var scanPage = Element as ScannerPage;
if (scanPage == null)
{
return;
}
scanPage.OnScanCompleted(result);
}
Hope this helps.-

Closing Xamarin forms Navigation Drawer

I just started with Xamarin forms and followed this example. But for the landscape mode it always has Navigation drawer opened. Is this default behavior? Below is my code
public class NavigationDrawer : MasterDetailPage // Navigation Drawer using MasterDetailPage
{
public override bool ShouldShowToolbarButton()
{
return true;
}
ContentPage gotoPage;
public NavigationDrawer()
{
Title = "Navigation Drawer Using MasterDetailPage";
string[] myPageNames = { "Camera2 Demo", "Second", "Third" };
SizeChanged += NavigationDrawer_SizeChanged;
ListView listView = new ListView
{
ItemsSource = myPageNames,
};
this.Master = new ContentPage
{
Title = "Options",
Content = listView,
Icon = "hamburger.png"
};
listView.ItemTapped += (sender, e) =>
{
switch (e.Item.ToString())
{
case "Camera2 Demo":
gotoPage = new CameraPage();
break;
case "Second":
gotoPage = new SecondPage();
break;
case "Third":
gotoPage = new ThirdPage();
break;
default:
gotoPage = new NavigationPage1();
break;
}
Detail = new NavigationPage(gotoPage);
((ListView)sender).SelectedItem = null;
this.IsPresented = true;
};
Detail = new NavigationPage(new HomePage());
IsPresented = false;
//// For Windows Phone, provide a way to get back to the master page.
//if (Device.OS == TargetPlatform.WinPhone)
//{
// (this.Detail as ContentPage).Content.GestureRecognizers.Add(
// new TapGestureRecognizer((view) =>
// {
// this.IsPresented = true;
// }));
//}
}
Question
1) How would I control opening and closing of Navigation Drawer? I found a way where it would give us control over the width of the navigation Drawer. Here is the Link. But is this the best option available right now?
1) Since project requires to be crossplaform Xamarin forms controls seems to be one of the option.
2) Should we go with Custom controls and not Xamarin forms controls?
I just started with Xamarin sample code would appreciate if someone can guide me through this.
1) How would I control opening and closing of Navigation Drawer?
Use this.IsPresented = true; to open and this.IsPresented = false; to close the drawer.
About the other questions I don't understand you good, but depending on your requirements, you should create custom controls or download one from NuGet.
Note:
I believe the most important thing for a beginner is to learn how to implement a native code using DependencyService and also to use Design patterns like MVVM.
You would need to set MasterBehavior = MasterBehavior.Popover on the MasterDetailPage to force it to exhibit the behaviour you are after, otherwise it will default to MasterBehavior.Default, which in Landscape mode will be always open.

Binding a HTML control into a stacklayout using xamarin forms

Being a newbie to Xamrin I am struggling to adding some HTML to a StackLayout via Xamarin Forms. I have tried quite a few things and had a Google around.
Firstly I can't work out which bindable object I am supposed to be using. As I cannot find a straight answer on Google/Xamarin I am going to assume this is not as easy I was hoping.
var nameEntry = new Label ();
nameEntry.SetBinding (Label.TextProperty, "Club.ClubName");
var webView = new WebView ();
webView.SetBinding ( ??? , "Club.Description");
var content = new StackLayout {
Children = {
nameEntry,
???
}
};
I am not sure if this is possible within Xamarin forms itself. Can anyone help?
I should point out my data for the form is being retrieved asynchronously on a remote json endpoint
protected override void OnAppearing ()
{
base.OnAppearing ();
if (ViewModel == null || ViewModel.IsLoading)
return;
ViewModel.LoadItemsCommand.Execute (Club.ClubId);
}
My remote json api contains, Description contatins a HTML snippet which I would like to use.
{
ClubName: "Stourbridge",
Description: "<p>This club meets every TWO weeks on a <b>Friday</b>.</p>"
...
}
Try the following example that will show how to do the bindings.
Note that you have to use a HtmlWebViewSource to achieve this, and bind the WebView.Source to this.
Clicking the button will change the view model and update the WebView appropriately to the newly changed text.
StackLayout objStackLayout = new StackLayout();
MyView objMyView = new MyView();
objMyView.MyHtml = "<html><head></head><body><h1>Title</h1><p>Some body text</p></body></html>";
HtmlWebViewSource objHtmlWebViewSource = new HtmlWebViewSource();
objHtmlWebViewSource.SetBinding(HtmlWebViewSource.HtmlProperty, "MyHtml");
objHtmlWebViewSource.BindingContext = objMyView;
WebView objWebview = new WebView();
objWebview.HorizontalOptions = LayoutOptions.FillAndExpand;
objWebview.VerticalOptions = LayoutOptions.FillAndExpand;
objWebview.Source = objHtmlWebViewSource;
Button objMyButton2 = new Button();
objMyButton2.Text="Change Html";
objMyButton2.Clicked+=((o2,e2)=>
{
objMyView.MyHtml = "<html><head></head><body><h1>Title</h1><p>Some body text that has changed.</p></body></html>";
});
objStackLayout.Children.Add(objMyButton2);
objStackLayout.Children.Add(objWebview);
The view model is just a simple one with a bindable property as below:-
public class MyView
: Xamarin.Forms.View
{
public static readonly BindableProperty MyHtmlProperty = BindableProperty.Create<MyView, string>(p => p.MyHtml, default(string));
public string MyHtml
{
get { return (string)GetValue(MyHtmlProperty); }
set { SetValue(MyHtmlProperty, value); }
}
}
Before clicking the button gives:-
After clicking the button, adjusts the view model, and automatically updates the control via the binding giving:-

NavigationService throwing AccessViolationException in c# on Windows Phone 8

Bear in mind I'm very new to programming. I've managed to set up images as custom markers on bing maps, the images are chosen and placed on the map according to data coming from a JSON feed. I have a list view page where one can view flood warnings, and clicking on an item in this list will take you to another page with further details of that particular flood. I want it so when someone clicks on a marker on the map it will take them to the info page corresponding to that flood. (It's a bit convoluted but I hope this is clear)
I've had great difficulty to get this to work, the code for navigating from the listview to the info page is simple enough,
private void listBoxBeaches_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
GetSelectedItem();
}
private void GetSelectedItem()
{
RootObject item = listBoxBeaches.SelectedItem as RootObject;
ArDe = item.AreaDescription;
SeTe = item.SeverityTxt;
Rais = item.RaisedF;
MesEng = item.MessageEnglish;
MesCym = item.MessageWelsh;
if (item != null)
{
NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
}
}
But in attempting to get the markers clickable so that it navigates to the same place has been problematic,
public string ArDe;
public string SeTe;
public string Rais;
public string MesEng;
public string MesCym;
public Grid marker;
public NavigationService navServ;
private Map myMap = new Map();
private MapLayer mylayer = new MapLayer();
public Map SetMapPins(List<RootObject>FloodList)
{
myMap.LandmarksEnabled = true;
myMap.PedestrianFeaturesEnabled = true;
myMap.Center = new GeoCoordinate(52.44, -4);
myMap.ZoomLevel = 7.8;
myMap.CartographicMode = MapCartographicMode.Road;
foreach (var flood in FloodList)
{
//this grabs the marker graphic
marker = flood.GetGrid();
MapOverlay myOverlay = new MapOverlay();
myOverlay.GeoCoordinate = new GeoCoordinate(flood.Center.Latitude, flood.Center.Longitude);
string AreaDes = flood.AreaDescription;
string SeverityTxt = flood.SeverityTxt;
string Raised = flood.Raised;
string EngMessage = flood.MessageEnglish;
string CymMessage = flood.MessageWelsh;
marker.MouseLeftButtonUp += (sender, args) => Floodpic_MouseLeftButtonUp(null, null, AreaDes, SeverityTxt, Raised, EngMessage, CymMessage);
myOverlay.Content = marker;
mylayer.Add(myOverlay);
}
myMap.Layers.Add(mylayer);
return myMap;
}
private void Floodpic_MouseLeftButtonUp(object sender, MouseButtonEventArgs e, string AreaDes, string SeverityTxt, string Raised, string EngMessage, string CymMessage)
{
GetSelectedMapItem(AreaDes, SeverityTxt, Raised, EngMessage, CymMessage);
}
public void GetSelectedMapItem(string AreaDes, string SeverityTxt, string Raised, string EngMessage, string CymMessage)
{
ArDe = AreaDes;
SeTe = SeverityTxt;
Rais = Raised;
MesEng = EngMessage;
MesCym = CymMessage;
//initially I used NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
//but this gives me "An object reference is required for the non-static field method or property 'System.Windows.Navigation.NavigationService.Navigate(System.Uri)" error
Navigate(navServ, new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}", ArDe, SeTe, Rais, MesEng, MesCym), UriKind.Relative));
}
public void Navigate(NavigationService s, Uri destination)
{
//This is where the AccessViolationException is thrown
s.Navigate(destination);
}
Just so it's clear, the listview code is navigating from the actual listview page (ListView.xaml.cs), while the marker code is not in the cs file of the page I'm navigating from (in SetMap.cs, not MapView.xaml.cs where the map and markers are) i.e. it navigates externally.
So I'm not sure what to do to get past this, I created the Navigation method due to getting an object reference is required error for
NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
even after trying
this.NavigationService.Navigate(new Uri(string.Format("/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
Now I'm getting the AccessViolationException thrown when Navigate is called. Any ideas?
EDIT
I've gone for a simpler solution for now (using CustomMessageBox, it gets the job done) but I'd still greatly appreciate a solution to this. I understand that this might be an incredibly specific problem and thus might require an equally specific answer. The code is a bit mish mashed but that's due to my lack of training or experience.
Try this path if your page is on root.
NavigationService.Navigate(new Uri(string.Format("~/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));
you can not call NavigationService.Navigate in constructor.
OR
if your are navigating from usercontrol.
RootFrame.Navigate(new Uri(string.Format("~/AlertInfoPage.xaml?area={0}&sev={1}&rais={2}&meseng={3}&mescym={4}",ArDe,SeTe,Rais,MesEng,MesCym) ,UriKind.Relative));

Categories

Resources