我在后面的contentview代码中实现了可绑定属性。我想在绑定属性后面访问该代码以查看视图,下面提到了我的代码,请参阅
CustomControlView.xaml
<ContentView.Content>
<StackLayout>
<Entry MyText="Text any message..." TextColor="Orange" />
</StackLayout>
</ContentView.Content>
CustomControlView.xaml.cs
public partial class CustomControlView : ContentView
{
public string MyText
{
get { return (string)GetValue(MyTextProperty); }
set { SetValue(MyTextProperty, value); }
}
// Using a DependencyProperty as the backing store for MyText. This enables animation, styling, binding, etc...
public static readonly BindableProperty MyTextProperty =
BindableProperty.CreateAttached("MyText", typeof(string), typeof(CustomControlView), "", defaultBindingMode: BindingMode.TwoWay, propertyChanged: MyTextPropertyChanged);
private static void MyTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (Entry)bindable;
control.Text = newValue.ToString();
}
}
MainView.xaml (内容页面)
xmlns:view="clr-namespace:MyApp.Views"
<ContextPage.Content>
<view:CustomControlView />
</ContentPage.Content>
我想将MyText属性设置为Entry。请给我任何建议。
答案 0 :(得分:0)
您需要在XAML中引用您的自定义视图。例如,如果您的CustomerControlView
在namespace MyApp.Controls
中,则需要在XAML中进行引用:
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp.Controls"
x:Class="MyApp.MainPage">
...
</ContentPage>
然后您像这样呼叫CustomerControlView
:
<local:CustomerControlView
MyText="Text any message..."
TextColor="Orange" />
最后,您的XAML如下所示:
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp.Controls"
x:Class="MyApp.MainPage">
<ContentPage.Content>
<StackLayout>
<local:CustomerControlView
MyText="Text any message..."
TextColor="Orange" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
仅供参考,在您的代码中,您将类称为CustomerControlView
,但在您的BindableProperty
中,您将引用CustomControlView
。
答案 1 :(得分:0)
首先像这样创建自定义控件,
public class MyCustomEntry : Entry
{
#region Property MyText
/// <summary>
/// Bindable Property MyText
/// </summary>
public static readonly BindableProperty MyTextProperty = BindableProperty.Create(
nameof(MyText),
typeof(string),
typeof(MyCustomEntry),
"",
BindingMode.TwoWay,
// validate value delegate
// (sender,value) => true
null,
// property changed, delegate
(sender,oldValue,newValue) => ((MyCustomEntry)sender).OnMyTextChanged(oldValue,newValue),
// property changing delegate
// (sender,oldValue,newValue) => {}
null,
// coerce value delegate
// (sender,value) => value
null,
// create default value delegate
// () => Default(T)
null
);
// <summary>
// On MyText changed
// </summary>
// <param name = "oldValue" > Old Value</param>
// <param name = "newValue" > New Value</param>
protected virtual void OnMyTextChanged(object oldValue, object newValue)
{
this.Text = newValue != null ? newValue.ToString() : oldValue.ToString();
}
/// <summary>
/// Property MyText
/// </summary>
public string MyText
{
get
{
return (string)GetValue(MyTextProperty);
}
set
{
SetValue(MyTextProperty, value);
}
}
#endregion
}
然后像这样在您的内容页面中使用它,
<local:MyCustomEntry
MyText="Text any message..."
TextColor="Orange" />