简体   繁体   English

在静态方法中使用非静态值?

[英]Using a non-static value in a static method?

I am currently using the sample app from https://github.com/xabre/xamarin-bluetooth-le , which shows data from a sensor in real-time.我目前正在使用https://github.com/xabre/xamarin-bluetooth-le 中的示例应用程序,它实时显示来自传感器的数据。 My goal now is to display a chart with this data, so I am using the Microcharts package.我现在的目标是用这些数据显示一个图表,所以我使用的是 Microcharts 包。 When using the following code with static data (and declaring the List as static) the chart works fine, but, when I try to use the real-time data (CharacteristicValue) I get the errors:将以下代码与静态数据一起使用(并将 List 声明为静态)时,图表工作正常,但是,当我尝试使用实时数据 (CharacteristicValue) 时,出现错误:

Error CS0120 An object reference is required for the non-static field, method, or property 'CharacteristicDetailViewModel.entries'错误 CS0120 非静态字段、方法或属性“CharacteristicDetailViewModel.entries”需要对象引用
Error CS0236 A field initializer cannot reference the non-static field, method, or property 'CharacteristicDetailViewModel.CharacteristicValueTemp'错误 CS0236 字段初始值设定项无法引用非静态字段、方法或属性“CharacteristicDetailViewModel.CharacteristicValueTemp”

I understand that this is happening because the data is changing, but I wonder if there is any workaround I could use to display the data in real-time?我知道这是因为数据正在发生变化,但我想知道是否有任何解决方法可以用来实时显示数据?

In my ViewModel I have:在我的 ViewModel 我有:

 public List<ChartEntry> entries = new List<ChartEntry>
     {
         new ChartEntry(CharacteristicValue)
         {
             Color=SKColor.Parse("#FF1943"),
             Label ="January",
             ValueLabel = "200"
         },
         new ChartEntry(400)
         {
             Color = SKColor.Parse("00BFFF"),
             Label = "March",
             ValueLabel = "400"
         },

     };

And in the .xaml.cs file:在 .xaml.cs 文件中:

 public CharacteristicDetailPage()
    {
        
        InitializeComponent();

        ChartTest.Chart = new LineChart() 
            {
                Entries = CharacteristicDetailViewModel.entries,
                LineMode = LineMode.Straight,
                LineSize = 8,
                PointMode = PointMode.Square,
                PointSize = 18,
            };
    }

(Yes, I know this chart makes no sense right now, I just wannna make the updating work before it gets more complicated) (是的,我知道这个图表现在没有意义,我只是想在它变得更复杂之前进行更新)

Thank you!谢谢! :) :)

CharacteristicDetailViewModel.entries is not a static member. CharacteristicDetailViewModel.entries不是静态成员。 You need a concrete instance of a CharacteristicDetailViewModel :您需要一个CharacteristicDetailViewModel的具体实例:

var myCharacteristicDetailVM = /*some initializing or retrieval logic of a concrete instance of the VM*/

And then:进而:

ChartTest.Chart = new LineChart() 
{
    Entries = myCharacteristicDetail.entries /*a concrete instance of the VM*/, 
    LineMode = LineMode.Straight,
    LineSize = 8,
    PointMode = PointMode.Square,
    PointSize = 18,
};

That said, you seem to have a deep misunderstanding about what static actually means in c#.也就是说,您似乎对static在 c# 中的实际含义有很深的误解。 I suggest you read up on the subject here .我建议你在这里阅读这个主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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