-3

I have this class

public partial class PhrasesFrame : Frame
{

    public CancellationTokenSource tokenSource1;
    public PhrasesFrameViewModel vm;

    public PhrasesFrame()
    {
        InitializeComponent();
        vm = new PhrasesFrameViewModel(this);
    }

and this view model

public class PhrasesFrameViewModel : ObservableProperty
{

    private readonly PhrasesFrame phrasesFrame;

    public PhrasesFrameViewModel(PhrasesFrame phrasesFrame) {
        this.phrasesFrame = phrasesFrame;
    }

    private void ResetTimer1()
    {
        if (phrasesFrame.tokenSource1 != null)

    }

on the if line where I use the value of tokenSource2 I get a message saying:

Error CS0120: An object reference is required for the non-static field, method, or property 'PhrasesFrameViewModel.phrasesFrame'

Can someone explain what I am doing wrong to me.

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Possible duplicate of [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) – rene Dec 24 '17 at 08:31
  • The code you show compiles fine: https://ideone.com/G2sJ4L – rene Dec 24 '17 at 08:43

1 Answers1

0

You want to use the phrasesFrame’s variable in a static function. A static function can’t use a class’s non-static variable. Just remove the static keyword and it will work.

Mátray Márk
  • 455
  • 5
  • 17