【问题标题】:Why is element I declared in XAML unreachable from C# code?为什么我在 XAML 中声明的元素无法从 C# 代码访问?
【发布时间】:2018-12-15 06:57:29
【问题描述】:

This is my project structure

我在 XAML (MainPage.xaml) 中声明 ListView:

<ListView x:Name="ShoppingListsLV"
              HorizontalOptions="CenterAndExpand"
              Margin="3"/>

并尝试从我的代码 (MainPage.xaml.cs) 中访问它:

ShoppingListsLV.ItemsSource = new string[] { "test", "test 2"};

我有一个错误:

CS0103:名称 ShoppingListsLV 在当前上下文中不存在

【问题讨论】:

标签: c# xamarin


【解决方案1】:

在调用后检查您是否正在编写代码:

InitializeComponent();

另外,检查您的视图是否引用了正确的代码隐藏文件:

&lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="YourNamespace.MainPage"&gt;

最后,尝试关闭 VS,然后从项目文件夹中删除以下文件夹:

Android - bin

Android - 对象

iOS - 垃圾箱

iOS - 对象

此操作将完全清除您的缓存。 完成此操作后,在 VS 中重新打开您的解决方案并执行清晰的重新构建操作。您的参考现在应该可以正常工作了。

【讨论】:

    【解决方案2】:

    将页面自定义工具属性更改为您的列表所在的 MSBuild:UpdateDesignTimeXaml。

    【讨论】:

      【解决方案3】:

      您的代码是正确的,如果您看到构建应用程序所需的错误,它应该可以工作并且您不会再看到错误。

      【讨论】:

        猜你喜欢
        • 2013-10-06
        • 2021-09-13
        • 1970-01-01
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多