C#某些公共属性不可访问,实际上完全丢失 [英] C# some public properties are not accessible, actually completely missing

查看:124
本文介绍了C#某些公共属性不可访问,实际上完全丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个具有3个公共属性的简单类,我有一个奇怪的问题。由于某种原因,即使定义它们的代码相同,也只有两个属性存在。

I have a bizarre problem with a simple class that has 3 public properties. For some reason, only 2 of the properties "exist" even though the code that defines them is identical.

在代码的其他地方,我将这三个属性绑定在一起,其中2个属性有效(度量单位和weightUnits),但 distanceUnits无效。

Elsewhere in the code I'm binding to these 3 properties, 2 of the properties work (metric & weightUnits), but "distanceUnits" does not.

当我在实例化此类的代码上放置断点并悬停时在该对象上,只有 metric和 weightUnits显示为公共属性,当我展开 non-public member时,除了 distanceUnits仍然存在。

When I put a breakpoint on the code where this class is instantiated, and hover over the object, only "metric" and "weightUnits" show up as public properties and when I expand "non-public members", everything is there but "distanceUnits" is still missing.

调试器屏幕截图:

public class AppGlobalSettings : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool _metric;
    public bool metric
    {
        get { return _metric; }
        set {
            _metric = value;
            if (_metric) {
                distanceUnits = "cm";
                weightUnits = "Kg";
            }
            else {
                distanceUnits = "in.";
                weightUnits = "lbs";
            }
            OnPropertyChanged();
        }
    }

    private string _weightUnits;
    public string weightUnits { get { return _weightUnits; } set { _weightUnits = value; OnPropertyChanged(); } }

    private string _distanceUnits;
    public string distanceUnits { get { return _distanceUnits; } set { _distanceUnits = value; OnPropertyChanged(); } }
...
}

我能想到的唯一皱纹是将该对象实例化为公共静态对象(请参见下文),但这并不能解释为什么只有某些属性可用...

The only wrinkle I can think of is that the object is instantiated as "public static (see below), but that wouldn't explain why only some properties are available...

public class App : Application
{
    public static AppGlobalSettings appSettings;
    public App() {
        appSettings = new AppGlobalSettings();
        appSettings.distanceUnits = "in."; // ** just for debugging **
...
}

我知道该属性存在并且是公共的,因为我放入了调试代码(如上所述)并且它可以工作(它确实调用了 distanceUnits的 set方法),但是没有显示

I know the property "exists" and is public because I put in debugging code (as noted above) and it works (it does indeed call the "set" method of "distanceUnits") but it does not show up in the debugger and the binding doesn't work.

为了完整起见,下面是显示distanceUnits的标签的绑定代码:

For the sake of completeness, here is the binding code for the labels that shows distanceUnits:

        Binding girthUnitBinding = new Binding("distanceUnits");
        girthUnitBinding.Source = App.appSettings;
        girthCell.unitLabel.SetBinding(Label.TextProperty, girthUnitBinding);

在该部分中,如果我将 distanceUnits编辑为 weightUnits,作为测试,

And in that section, if I edit "distanceUnits" to "weightUnits" just as a test, the binding works.

那么,为什么propertyChanged处理程序和调试器可以看到 weightUnits但看不到 distanceUnits?

So any idea why the propertyChanged handler and the debugger can see "weightUnits" but not "distanceUnits" ?

推荐答案

感谢Ed Plunkett对这实际上是一个构建/调试问题的评论,我能够找到此问题的解决方案,所以我想我会自己回答问题,以防万一有人偶然发现了这个问题,即使原始问题和最终答案似乎完全无关。

Thanks to the comments from Ed Plunkett about this actually being a build/debug issue, I was able to find a solution to this problem so I thought I'd answer my own question just in case anyone stumbles on this thread even though the original question and the final answer seem totally unrelated.

我的解决方案受到了这篇文章的启发:
< a href = https://forums.xamarin.com/discussion/45327/newest-version-of-code-not-always-deployed-when-debugging-from-xamarin-studio rel = nofollow noreferrer> https ://forums.xamarin.com/discussion/45327/newest-version-of-code-not-always-deployed-when-debugging-from-xamarin-studio

My solution was inspired by this post: https://forums.xamarin.com/discussion/45327/newest-version-of-code-not-always-deployed-when-debugging-from-xamarin-studio

简而言之:


  1. 关闭Visual Studio

  2. 打开Windows资源管理器并导航到您的解决方案文件夹

  3. 删除以下每个文件夹的内容:


    • 发布/ obj

    • 发布/ bin

    • 调试/ bin

    • Debug / obj

  1. Shutdown Visual Studio
  2. Open windows explorer and navigate to your solution folder
  3. Delete the contents of each of these folders:
    • Release/obj
    • Release/bin
    • Debug/bin
    • Debug/obj

  • projectname

  • projectname .Droid

  • 项目名。iOS

  • 等。

  • projectname
  • projectname.Droid
  • projectname.iOS
  • etc.

注意:您会认为构建->清洁解决方案(或类似方法)会为您完成此操作,但不会。您必须手动进入并自己删除文件。

Note: You would think that "Build -> Clean Solution" (or similar) would do this for you but it does not. You have to go in manually and delete the files yourself.

希望这对某人有帮助,并再次感谢Ed为我提供了正确的起点。

Hope this helps someone and thanks again to Ed for starting me down the right path.

这篇关于C#某些公共属性不可访问,实际上完全丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆