是否可以在void set_method(const char *method)
{
// Check if the method is serial_port
if (strcmp(method, "serial_port") == 0)
{
// assign the alias "print" to serial_print
// something like defing here a function like this:
// print(const char *print) { serial_print(print); }
print(const char *print) = serial_print(const char *message)
}
else if (strcmp(method, "graphical_tty") == 0)
{
// The same that serial_port case but with graphical_tty_print
}
else
{
// Error
}
}
中创建类似的侧边菜单?
答案 0 :(得分:2)
我搜索一个小菜单。将大小宽度修改为MasterPage
您可以更改Master
中的MasterDetailPageRenderer
页面的宽度。
首先,在自定义BindableProperty
类中创建MasterDetailPage
:
public class MyMasterDetailPage : MasterDetailPage
{
public static readonly BindableProperty DrawerWidthProperty =
BindableProperty.Create(
"WidthRatio",
typeof(int),
typeof(MyMasterDetailPage),
(float)0.2,
propertyChanged: (bindable, oldValue, newValue) =>
{
});
public float WidthRatio
{
get { return (float)GetValue(WidthRatioProperty); }
set { SetValue(WidthRatioProperty, value); }
}
}
其次,在MasterDetailPageRenderer
中,设置MyMasterDetailPage
这样的宽度:
[assembly: ExportRenderer(typeof(MyMasterDetailPage), typeof(MyMasterDetailPageRenderer))]
...
public class MyMasterDetailPageRenderer : MasterDetailPageRenderer
{
protected override void OnElementChanged(VisualElement oldElement, VisualElement newElement)
{
base.OnElementChanged(oldElement, newElement);
var width = Resources.DisplayMetrics.WidthPixels;
var fieldInfo = GetType().BaseType.GetField("_masterLayout", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var _masterLayout = (ViewGroup)fieldInfo.GetValue(this);
var lp = new DrawerLayout.LayoutParams(_masterLayout.LayoutParameters);
MyMasterDetailPage page = (MyMasterDetailPage)newElement;
lp.Width = (int)(page.WidthRatio * width);
lp.Gravity = (int)GravityFlags.Left;
_masterLayout.LayoutParameters = lp;
}
}
用法,在我的App.xaml.cs
中,我设置了这样的页面:
public App()
{
InitializeComponent();
var mdp = new MyMasterDetailPage
{
Master = new MenuPage() { Title = "Master" },
Detail = new ContentPage
{
BackgroundColor = Color.White,
Title = "DetailPage",
Content = new Label
{
Text = "DetailPage",
HorizontalTextAlignment = TextAlignment.Center,
VerticalTextAlignment = TextAlignment.Center,
},
},
WidthRatio = 0.5f,
};
MainPage = mdp;
}
关于Xamarin.iOS
,您可以参考受this启发的P3PPP's project,您唯一需要做的就是在MyPhoneMasterDetailRenderer.cs更改此行:
//Change 0.8 to whatever you want
masterFrame.Width = (int)(Math.Min(masterFrame.Width, masterFrame.Height) * 0.8);
答案 1 :(得分:0)