如何解决或忽略ReSharper对Xamarin.Forms自定义属性的“无法解析符号”错误
解决ReSharper误报Xamarin.Forms自定义BindableProperty不存在的问题
我之前也碰到过ReSharper在Xamarin.Forms项目里误报自定义BindableProperty找不到的情况,结合你的场景(VS 2017 + ReSharper Ultimate 2018.1.1),给你几个可行的解决办法:
优先尝试修复识别问题(不推荐直接忽略)
这些方法能从根源解决ReSharper的误判,比直接忽略错误更靠谱:
- 清理ReSharper缓存:这是最常见的解决办法。操作路径:VS顶部菜单栏 →
ReSharper→Options→Environment→General→ 点击「Clear Caches」,然后重启VS。很多时候缓存紊乱会导致这类识别错误。 - 优化BindableProperty定义:虽然你的代码看起来没问题,但可以用
nameof语法替代硬编码字符串,既避免拼写错误,也能让ReSharper更准确关联属性和绑定字段:public static readonly BindableProperty InitializationStepProperty = BindableProperty.Create(nameof(InitializationStep), typeof(int), typeof(InitPage), 0); public int InitializationStep { get { return (int)GetValue(InitializationStepProperty); } set { SetValue(InitializationStepProperty, value); } } - 验证XAML类关联:确认XAML里的
x:Class属性完全匹配后台类的命名空间和类名(MyApp.Views.InitPage),没有拼写错误,同时x:Name="ctrl"的命名也没有冲突。 - 重新生成解决方案:右键解决方案 →
Clean Solution,然后Rebuild Solution,强制重新编译所有文件,让ReSharper重新索引代码。
若修复无效,设置忽略该错误(解决构建失败问题)
如果上面的方法都没效果,你可以通过以下两种方式让ReSharper不再报这个错误:
方法1:在XAML中局部忽略
给报错的属性添加ReSharper注释禁用检查,只影响当前属性,不会全局关闭规则:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.Views.InitPage" x:Name="ctrl"> <!-- ReSharper disable Xaml.BindablePropertyNotFound --> InitializationStep="{Binding InitProgressStep}" <!-- ReSharper restore Xaml.BindablePropertyNotFound --> </ContentPage>
方法2:全局禁用该检查
如果这类误报频繁出现,可以全局关闭这个规则:
- 打开ReSharper设置:VS顶部菜单栏 →
ReSharper→Options - 导航到
Code Inspection→Inspection Severity - 在搜索框输入「Bindable property not found」
- 把对应的规则严重级别改成「Do not show」或者「Hint」,这样就不会影响构建系统了
额外提示
ReSharper 2018.1.1属于比较老的版本,对Xamarin.Forms的BindableProperty识别可能存在已知bug,如果条件允许,建议升级到更高版本的ReSharper,能减少这类误报的发生。
内容的提问来源于stack exchange,提问作者Thalox
相关产品推荐
相关产品推荐

