【发布时间】:2018-09-22 03:04:26
【问题描述】:
我正在使用 Xamarin,它要求我的 CS 类和 XAML 继承自这样的 Xamarin 对象:
CS
namespace Japanese.Templates
{
public partial class TimeIntervalTemplate : ContentView
{
public TimeIntervalTemplate()
{
InitializeComponent();
}
// All my time templates contain this and I would
// like to not have to repeat these many times in
// each time template
public static readonly BindableProperty SelectedValProperty =
BindableProperty.Create(
"SelectedVal", typeof(string), typeof(CardOrderTemplate),
defaultBindingMode: BindingMode.TwoWay,
defaultValue: default(string));
// All my time templates contain this and I would
// like to not have to repeat these many times in
// each time template
public string SelectedVal
{
get { return (string)GetValue(SelectedValProperty); }
set { SetValue(SelectedValProperty, value); }
}
XAML
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Japanese;assembly=Japanese"
xmlns:b="clr-namespace:Behaviors;assembly=Behaviors"
x:Class="Japanese.Templates.TimeIntervalTemplate" x:Name="this">
<StackLayout BackgroundColor="#FFFFFF" Padding="20,0" HeightRequest="49" Margin="0">
但是这里的相同属性和对象用于许多不同的类:
我想做的是简单地创建一个继承自 Content 视图的 BaseTemplate 类。向其中添加属性,然后让 TimeIntervalTemplate 从 BaseTemplate 继承。但是,当我这样做时:
public class BaseTemplate : ContentView
...
public partial class TimeIntervalTemplate : BaseTemplate
...
然后它告诉我我不能这样做,因为部分类必须从同一个基类继承。
有没有办法解决这个问题?无论如何,我可以添加诸如SelectedValProperty .. 之类的属性,而无需从基类继承?
【问题讨论】:
标签: c# xamarin xamarin.forms