I have a layout coded in c#, which is a "menu" that I want to use in all my other activities. Is there a way to include or reuse this layout in my other pages?
Asked
Active
Viewed 480 times
0
-
What type of Class is the layout or what does it inherit from? Is it a `StackLayout`, a `ContentView`, etc.? You should be able to put the layout into it's own class and then just reference the class from other pages in your app. – hvaughan3 Jan 11 '17 at 19:34
-
its a stacklayout – Gustavo Serna Jan 11 '17 at 19:36
-
You can inherit MasterDetailPage url: https://developer.xamarin.com/guides/xamarin-forms/user-interface/navigation/master-detail-page/ – Junior Porfirio Jan 11 '17 at 19:45
1 Answers
1
You would just add your StackLayout
to its own class and then reuse it where ever you want. If you want to get fancy and add bindable properties and stuff like there, there are some example of that here for Xamarin's guide or another example here.
namespace App.Controls {
public class CustomMenu : StackLayout {
//Custom stuff here
}
}
Then use it in your XAML:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:App.Controls;assembly=App"
x:Class="App.Pages.MyMenuPage">
<controls:CustomMenu/>
</ContentPage>
Or in C#:
public class MyContentPage : ContentPage {
public MyContentPage() { Content = new CustomMenu(); }
}
-
-
@GustavoSerna The layout could be in C# (which it is in my example) but you can use it where ever you want. Do you need to see the C# layout being used in C# code? Just call the class... – hvaughan3 Jan 11 '17 at 22:10
-
-
1for a real-world example, [these views](https://github.com/michael-watson/Forms-Expenses/tree/master/MyExpenses/Views) are created in C# and reused in [these C# pages](https://github.com/michael-watson/Forms-Expenses/tree/master/MyExpenses/Pages). – BrewMate Jan 11 '17 at 23:27