简体   繁体   中英

C# some public properties are not accessible, actually completely missing

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.

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

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.

Debugger screen shot:

在此处输入图片说明

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 **
...
}

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.

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);

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

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

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.

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

In short:

  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
  4. If your solution has multiple projects, for example if you are developing cross-platform solutions using Xamarin, you'll potentially need to do this for each project folder in the solution, eg:
    • 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM