繁体   English   中英

C#错误:非静态字段,方法或属性需要对象引用

[英]C# Error: Object reference is required for the non-static field, method, or property

我需要一点帮助。 我试图在我的WPF应用程序中填充纬度和经度值2个textboxes 现在,我可以将值显示在MessageBox并以所需的格式设置它们在文本框中的显示方式。

但是,当我尝试将数据发送到文本框时,

“非静态字段,方法或属性'Registration.tbXCoords'需要对象引用。

任何帮助将不胜感激。

//Registration.xaml.cs     

using System.Windows;
using System.Device.Location;
using System;

namespace Battle_Sample
{

public partial class Registration : Window
{
    public Registration()
    {
        WindowStartupLocation = WindowStartupLocation.CenterScreen;
        InitializeComponent();
        CLocation myLocation = new CLocation();
        myLocation.GetLocationEvent();
    }

    public class CLocation
    {
        GeoCoordinateWatcher watcher;

        public void GetLocationEvent()
        {
            this.watcher = new GeoCoordinateWatcher();
            this.watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            bool started = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));
            if (!started)
            {
                MessageBox.Show("GeoCoordinateWatcher timed out on start.");
            }
        }

        public void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            PrintPosition(e.Position.Location.Latitude, e.Position.Location.Longitude);
        }

        public void PrintPosition(double Latitude, double Longitude)
        {
            MessageBox.Show("X: " + Latitude.ToString("0.00") + ", Y: " + Longitude.ToString("0.00"));

            //The following two lines are where I am getting the error
            tbXCoords.Text = (Latitude.ToString());
            tbYCoords.Text = (Longitude.ToString());
        }
    }
  }
}




//Registration.xaml

<Window x:Class="Battle_Sample.Registration"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Battle_Sample"
    mc:Ignorable="d"
    Title="Battle Registration" Height="300" Width="300">

<Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="4*"/>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <StackPanel Grid.Column="0" Grid.Row="3">
        <TextBlock FontWeight="Bold" Text="Location:" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,6,0,0" />
    </StackPanel>

    <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="3">
        <TextBlock Margin="2" VerticalAlignment="Center">X</TextBlock>
        <TextBox x:Name="tbXCoords" Margin="5" Width="50" IsEnabled="False" />
    </StackPanel>

    <StackPanel Orientation="Horizontal" Grid.Column="2" Grid.Row="3">
        <TextBlock Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center">Y</TextBlock>
        <TextBox x:Name="tbYCoords" Margin="5" Width="50" IsEnabled="False" />
    </StackPanel>
</Grid>

tbXCoordstbYCoords是您的Registration类中定义的字段,并且由于错误消息告诉您,它们不是静态的,因此要访问它们,您需要指定对“拥有”它们的Registration对象的实际引用。

实际上,您正在做的事情等同于:

public class A
{
    public string Hello = "Hello";

    public class B
    {
        //Hello of what A?
        public void SayHello() { 
            Console.Write(Hello); } //Error  
    }
}

这显然是错误的。 您需要做的是:

public class B
{
    //Specify what specific A's Hello you want.
    public void SayHello(/*A a*/) {
        Console.Write(a.Hello); }   
}

a可以是局部变量,字段,方法参数,属性等,但它必须是指向具体A对象的变量(或空引用,在这种情况下,您的代码将编译但在运行时失败)。

为了完全理解您收到的错误消息,请考虑Hello静态的情况:

public class A
{
    public static string Hello = "Hello";

    public class B
    {
        public void SayHello() { 
            Console.Write(Hello); }  
    }
}

现在,因为该领域这段代码编译就好Hello现在是“拥有”的类型A通过具体实例本身,而不是A ; 对于所有A来说,只有一个Hello (这是对静态字段的真正非正式描述,但是应该能对整个静态图片有一个合理的认识)。

好吧,足够聊天了,您如何解决呢? 好吧,在不了解太多代码的情况下,一种合理的方法是通过构造函数告诉具体的CLocation实例具体的Registration拥有它:

public Registration()
{
    WindowStartupLocation = WindowStartupLocation.CenterScreen;
    InitializeComponent();
    CLocation myLocation = new CLocation(this);
    myLocation.GetLocationEvent();
}

public class CLocation
{
     private readonly Registration registrationWindow;

     public CLocation(Registration registrationWindow) {
         this.registrationWindow = registrationWindow; }

     //...

    public void PrintPosition(double Latitude, double Longitude)
    {
        //...
        registrationWindow.tbXCoords.Text = (Latitude.ToString());
        registrationWindow.tbYCoords.Text = (Longitude.ToString());
    }
}

现在,如果仅在PrintPosition方法中需要引用,则这可能是一个PrintPosition 在这种情况下,您可以考虑将具体的窗口实例指定为参数:

public void PrintPosition(Registration registrationWindow,
                          double Latitude, 
                          double Longitude)
{
    //...
    registrationWindow.tbXCoords.Text = (Latitude.ToString());
    registrationWindow.tbYCoords.Text = (Longitude.ToString());
}

同样,您应该知道哪种方法更适合您的特定情况。

我不知道您为什么要在不同的类中放入“ GeoLocation”代码。 但是您可以使用事件来解决此问题:

public partial class Registration: Window
{
    public Registration()
    {
        InitializeComponent();
        WindowStartupLocation = WindowStartupLocation.CenterScreen;
        InitializeComponent();
        CLocation myLocation = new CLocation();
        myLocation.OnPositionChanged += new EventHandler<PositionEventArgs>(myLocation_OnPositionChanged);
        myLocation.GetLocationEvent();

    }

    private void myLocation_OnPositionChanged(object sender, PositionEventArgs e)
    {
        tbXCoords.Text = (e.Latitude.ToString());
        tbYCoords.Text = (e.Longitude.ToString());
    }

    public class CLocation
    {
        public EventHandler<PositionEventArgs> OnPositionChanged;
        GeoCoordinateWatcher watcher;

        public void GetLocationEvent()
        {
            watcher = new GeoCoordinateWatcher();
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            bool started = this.watcher.TryStart(false, TimeSpan.FromMilliseconds(1000));
            if (!started)
            {
                MessageBox.Show("GeoCoordinateWatcher timed out on start.");
            }
        }

        public void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            OnPositionChanged?.Invoke(this, new PositionEventArgs() { Latitude = e.Position.Location.Latitude, Longitude = e.Position.Location.Longitude });
        }
    }
}

这是PostionEventArgs类:

public class PositionEventArgs
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

暂无
暂无

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

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