回答您的实际问题:不,您不能像您的示例所示,更改传入属性值的方式,而不更改该属性定义方式背后的代码。现在谈谈你的实际问题。。。
我将字符串传递到用户控件上URI类型的属性中,并将其从字符串自动转换为URI,没有任何问题。您确定要传递的Uri有效吗?如果您传入的字符串(如在数据绑定场景中)格式不正确,我可能会看到出现此问题。
<uc1:WebUserControl ID="WebUserControl1" runat="server" MyUrlProperty="http://www.example.com" />
代码隐藏:
Partial Class WebUserControl
Inherits System.Web.UI.UserControl
Public Property MyUrlProperty() As Uri
Get
Dim o As Object = ViewState("m")
If o IsNot Nothing Then
Return DirectCast(o, Uri)
Else
Return Nothing
End If
End Get
Set(ByVal value As Uri)
ViewState("m") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Write(MyUrlProperty)
End Sub
End Class
--彼得