How to convert XAML to pure C#? - c#

I have page in XAML and need it in pure C#. But can't figure out how to convert it.
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="xmr_cross_test.Navigation.MDPage">
<MasterDetailPage.Master>
<ContentPage Title="Menu">
<StackLayout Orientation="Vertical">
<ListView x:Name="navigationDrawerList"
RowHeight="60"
SeparatorVisibility="None"
ItemSelected="OnMenuItemSelected">
</ListView>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
</NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
How to do so keeping binding and ability to change MasterDetailPage.Detail?
I had to remove ListView.ItemTemplate declaration as SO doesn't allow me to post so big amount of code. Guess it won't be too hard to figure out after I get answer.

MasterDetailPage mdp = new MasterDetailPage();
ContentPage master = new ContentPage { Title = "Menu" };
StackLayout menu = new StackLayout();
ListView menuList = new ListView() { RowHeight = 60 };
menuList.ItemSelected += OnMenuItemSelected;
ContentPage detail = new ContentPage();
menu.Children.Add(menuList);
master.Content = menu;
mdp.Master = master;
mdp.Detail = new NavigationPage(detail);

Related

i want to make the button visible when clipboard has number when the app starts ( xamarin)

when i call this method the clipboard value is null
****here is the method ****
async void clip()
{
var phoneNum = await Clipboard.GetTextAsync();
if (Clipboard.HasText)
{
//btnSaudi.Text = phoneNume;
Regex reg = new Regex(#"^\d+$");
if (reg.IsMatch(phoneNum))
btnSaudi.IsVisible = true;
}
}
but when i use the same code in button click event it works
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DirectM.MainPage"
Title="DirectN">
<ContentPage.ToolbarItems>
<ToolbarItem Text="setting" Clicked = "ToolbarItem_Clicked" Order="Secondary" />
</ContentPage.ToolbarItems>
<StackLayout >
<StackLayout Padding="20,50,20,10" >
<Entry x:Name="txtPhone" Keyboard="Telephone" Placeholder="numbe" />
</StackLayout>
<StackLayout Padding="50,50,50,100" >
<Button x:Name="btnSaudi" Text="ENTER" Clicked="btnSaudi_Clicked" Padding="5,5,5,5" FontSize="Large" />
</StackLayout>
</StackLayout>
</ContentPage>
and this is the xaml code for the page
You can override the onAppearing() method. This method is called before page become visible.Code like:
protected override void OnAppearing()
{
base.OnAppearing();
clip();
}
async void clip()
{
var phoneNum =await Clipboard.GetTextAsync();
if (Clipboard.HasText)
{
Regex regex = new Regex(#"^\d+$");
Console.WriteLine(phoneNum);
if (regex.IsMatch(phoneNum))
{
Console.WriteLine("match");
btnSaudi.IsVisible = true;
}
}
}
Screenshots:

NavigationPage + CarouselView. Error: Unable to activate instance

I used MasterDetailPage. And navigation with NavigationPage.
This simple is good work before add in project CarouselView.
public List<MasterPageItem> menuList { get; set; }
public MainPage()
{
InitializeComponent();
menuList = new List<MasterPageItem>();
menuList.Add(new MasterPageItem() { Title = "Главная", Icon = "home.png", TagetType = typeof(HomePage) });
menuList.Add(new MasterPageItem() { Title = "Расписание", Icon = "calendar.png", TagetType = typeof(DiaryPage) });
menuList.Add(new MasterPageItem() { Title = "Новости", Icon = "news.png", TagetType = typeof(NewsPage) });
menuList.Add(new MasterPageItem() { Title = "Профиль", Icon = "profile.png", TagetType = typeof(ProfilePage) });
navigationDrawerList.ItemsSource = menuList;
Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(TestPage)));
}
private void onMenuItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (MasterPageItem)e.SelectedItem;
Type page = item.TagetType;
Detail = new NavigationPage((Page)Activator.CreateInstance(page));
IsPresented = false;
}
if open page include CarouselView and property VerticalOptions="FillAndExpand" then visible exception
System.NotSupportedException: Unable to activate instance of type Com.ViewPagerIndicator.CirclePageIndicator
from native handle 0x7fc5472274 (key_handle 0x584e8e5).
xaml file
<?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:carousel="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions"
x:Class="AppMobile.Views.HomePage"
Title="Home">
<ContentPage.Content>
<StackLayout>
<StackLayout HeightRequest="300">
<carousel:CarouselViewControl x:Name="MyCV" BackgroundColor="Green" VerticalOptions="FillAndExpand" >
<!--ShowIndicators="True" -->
<carousel:CarouselViewControl.ItemTemplate>
<DataTemplate>
<StackLayout BackgroundColor="Blue">
<Image x:Name="myImg" Source="{Binding FileName}" Aspect="AspectFill" />
</StackLayout>
</DataTemplate>
</carousel:CarouselViewControl.ItemTemplate>
</carousel:CarouselViewControl>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
if exclude property VerticalOptions="FillAndExpand" then content in CarouselViewControl is empty. Why use both component with out error?

Xamarin.Forms: Map.IsShowingUser not showing current location?

This is the xaml for the app I'm working on. It simply displays a map on the screen with a ListView:
<?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:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:local="clr-namespace:GasStations"
x:Class="GasStations.MainPage">
<StackLayout>
<StackLayout VerticalOptions="FillAndExpand">
<maps:Map WidthRequest="960" HeightRequest="200"
x:Name="MyMap"
IsShowingUser="true"/>
<ListView x:Name="ListView_Pets">
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>dog</x:String>
<x:String>cat</x:String>
<x:String>bird</x:String>
</x:Array>
</ListView.ItemsSource>
</ListView>
</StackLayout>
</StackLayout>
</ContentPage>
I set the property Map.IsShowingUser="true" thinking that it would display the current location, but it's not showing the point.
This is what the form looks like. It doesn't display the current location pin.
My question is: how can I add current location to the map?
Use the Geolocation plugin to get your current location. If you don't have it already and then use the MoveCamera method to move to that position:
private async Task TrackCurrentLocation()
{
var current = await CrossGeolocator.Current.GetPositionAsync();
current.Accuracy = 30;
float bearing =//bearing in case any
var pins = new Pin { Label = "ME", Icon =''yourIcon"), Flat = true
};
var latitude = current.Latitude;
var longitude = current.Longitude;
Device.BeginInvokeOnMainThread(() =>
{
pins.Position = new Position(latitude, longitude);
pins.Rotation = bearing;
if (MapTrack.Pins.Count == 0)
{
MapTrack.Pins.Add(pins);
}
else
{
MapTrack.Pins[0] = pins;
}
MapTrack.MoveCamera(CameraUpdateFactory.NewPosition(new Position(latitude, longitude)));
});
}

Cannot see the Master Section in Masterdetail page of Xamarain

I am trying to get into Xamarain development and I am trying to do a small App which have the login screen and when the user press the screen it will be navigated to a Dashboard Which is actually a Master Details Page
My master details Page is as follows
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MobArt.View"
x:Class="MobArt.View.MasterFrontPage">
<MasterDetailPage.Master>
<ContentPage Tittle="Menu">
<StackLayout Orientation="Vertical">
<Button Text="WF"></Button>
<Button Text="FW"></Button>
<Button Text="RO Out"></Button>
<Button Text="RO IN"></Button>
<Button Text="Loan Out"></Button>
<Button Text="Loan IN"></Button>
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<local:RollScan></local:RollScan>
</MasterDetailPage.Detail>
</MasterDetailPage>
And Codebehind is just constructor
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MasterFrontPage : MasterDetailPage
{
public MasterFrontPage()
{
InitializeComponent();
}
}
And in LoginButtonclick I tried to navigate using below cODE
async void OnLoginButtonClicked(object sender, EventArgs e)
{
var Username = usernameEntry.Text;
var Password = passwordEntry.Text;
UserServices usrserv = new UserServices();
User usr = await usrserv.GetSelecteduserdataASync(Username, Password);
if(usr.User_PK!=0&& usr.UserLoc_PK!=0)
{
MasterDetailPage fpm = new MasterFrontPage();
Application.Current.MainPage = fpm;
}
}
Iam able to See the Details Section in my Page.But cannot see the Menu(Master section) when I tried for Android. Can Anyone suggest what I am missing
I am able to See the Details Section in my Page.But cannot see the Menu(Master section) when I tried for Android. Can Anyone suggest what I am missing
The Menu(Master Section) is still there, but is hidden. You can get it by swipping from the left:
But it is highly recommended using NavigationPage as the details Page:enter code here
...
<MasterDetailPage.Detail>
<NavigationPage>
<x:Arguments>
<local:RollScan />
</x:Arguments>
</NavigationPage>
</MasterDetailPage.Detail>
...
Thus, you will have a toggle button for your menu:

Binding text via a function

I am developing an app using Xamarin, and currently trying to display the screen width to the (I later plan to base sizes of certain objects off this)
I am attempting to use bindings to do this, but unfortunately it doesn't appear to work as expected. It does not throw an exception, just the label that I am attempting to bind has no text value.
I am sure I will be using bindings quite a lot during the rest of the project, so I would be greatly appreciative of any advice.
Please consider the following code:
C#:
using Xamarin.Forms;
using XLabs.Ioc;
using XLabs.Platform.Device;
namespace TimerStyles
{
public partial class SavedTimesPage : ContentPage
{
string screenWidthText = "";
public SavedTimesPage()
{
InitializeComponent();
this.screenWidth = getScreenWidth();
}
public string screenWidth
{
protected set
{
screenWidthText = value;
}
get { return screenWidthText; }
}
public string getScreenWidth()
{
var dev = Resolver.Resolve<IDevice>();
var display = dev.Display;
double ScreenWidthInches = (double)display.Width / display.Xdpi;
var ScreenWidth = (ScreenWidthInches.ToString());
return ScreenWidth;
}
public string getScreenHeight()
{
var dev = Resolver.Resolve<IDevice>();
var display = dev.Display;
double ScreenHeightInches = (double)display.Height / display.Ydpi;
var ScreenHeight = (ScreenHeightInches.ToString());
return ScreenHeight;
}
}
}
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:TimerStyles="clr-namespace:TimerStyles;assembly=TimerStyles"
x:Class="TimerStyles.SavedTimesPage"
NavigationPage.HasNavigationBar="false">
<ContentPage.Content>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" BackgroundColor="#232224">
<Label Text="x" TextColor="Blue" FontSize="Large"/>
<Label Text="{Binding screenWidth}" TextColor="Blue" FontSize="Large"/>
<Label Text="x" TextColor="Blue" FontSize="Large"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
This was solved by creating a "view-model" class to act as an intermediary between the view and the model, as per the following link:
https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_bindings_to_mvvm/
(Obtained by following further on the link given in comments by cristallo)
Hopefully this will help anyone in the space-age future with the same issue.

Categories

Resources