Vb.net 不同页面上的2个相同从属属性

Vb.net 不同页面上的2个相同从属属性,vb.net,windows-runtime,windows-phone-8.1,dependency-properties,Vb.net,Windows Runtime,Windows Phone 8.1,Dependency Properties,我声明了一个名为H1Property的DependencyProperty,如下所示: Public Shared ReadOnly H1Property As DependencyProperty = DependencyProperty.Register("H1", GetType(String), GetType(Button), Nothing) Public Property H1() As String Get Return DirectCast(GetVal

我声明了一个名为H1Property的DependencyProperty,如下所示:

Public Shared ReadOnly H1Property As DependencyProperty = DependencyProperty.Register("H1", GetType(String), GetType(Button), Nothing)
Public Property H1() As String
    Get
        Return DirectCast(GetValue(H1Property), String)
    End Get
    Set(ByVal value As String)
        SetValue(H1Property, value)
    End Set
End Property    
在第1页,我使用它为带有自定义模板的按钮分配一个值(例如template1,需要在不同的页面上使用相同的模板,因此它存储在App.xaml中)。我在项目中还有另一个页面(第2页),在代码隐藏中具有相同的属性。当我在page1上使用template1动态添加按钮时,效果很好,但当我导航到page2,然后返回page1并再次生成控件时,新按钮中的值为空。没有错误,只有空字段

有什么问题?有没有办法只声明一次DependencyProperty,然后在不同的页面上使用它


提前感谢。

您可以使用一个模块来存储公共状态并在页面中引用它

Public Module CommonState
    Public ReadOnly H1Property As DependencyProperty = _
        DependencyProperty.Register("H1", GetType(String), GetType(Button), Nothing)
End Module
在两页上

Public Property H1() As String
    Get
        Return DirectCast(GetValue(CommonState.H1Property), String)
    End Get
    Set(ByVal value As String)
        SetValue(CommonState.H1Property, value)
    End Set
End Property    
如果页面本身需要
h1属性
,可以将其包装为属性

Public Shared ReadOnly Property H1Property() As DependencyProperty
   Get
      Return CommonState.H1Property
   End Get
End Property