C# System.Deviceがない

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
dic
記事: 658
登録日時: 14年前
住所: 宮崎県
連絡を取る:

C# System.Deviceがない

#1

投稿記事 by dic » 8年前

[1] 質問文
 [1.1] 自分が今行いたい事は何か
  自分の位置情報を知りたい
  そのために、System.Device を using で使いたいがでてこない

 [1.2] どのように取り組んだか(プログラムコードがある場合記載)
  なし

 [1.3] どのようなエラーやトラブルで困っているか(エラーメッセージが解る場合は記載)
  System.Deviceが見つからない
  http://internetcom.jp/developer/20100622/26.html のページには
  C:?Program Files?Microsoft SDKs?Windows?v7.0 の Bin フォルダにあると書いているがない

 [1.4] 今何がわからないのか、知りたいのか
  System.Device を新たに追加インストールする方法、サイトがわからない。

[2] 環境  
 [2.1] OS : Windows, Linux等々
  Windows7 Professional 32bit

 [2.2] コンパイラ名 : VC++ 2008EE, Borand C++, gcc等々
Visual C# 2010 Express Edition

[3] その他
 ・どの程度C言語を理解しているか
よく理解している

 ・ライブラリを使っている場合は何を使っているか
  .NET4 を使用しないといけない(?)

dic
記事: 658
登録日時: 14年前
住所: 宮崎県
連絡を取る:

Re: C# System.Deviceがない

#2

投稿記事 by dic » 8年前

すいません、ある程度わかりました。

コード:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Device.Location;
using System.Threading;

namespace pokemon
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            GeoCoordinateWatcher wtc = new GeoCoordinateWatcher();
            wtc.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(wtc_PositionChanged);
            wtc.Start(); 
        }
        void wtc_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            Dispatcher.BeginInvoke(() =>
                textBox1.Text =
                e.Position.Location.Latitude.ToString() + ", " +
                e.Position.Location.Longitude.ToString()
                );
        }
    }
}

ここまでできたのですが、エラーが以下の通りにでます

名前 Dispatcher は現在のコンテキスト内にありません。

using System.Threading; しているのですが、このエラーがなくなりません。

C6b14

Re: C# System.Deviceがない

#3

投稿記事 by C6b14 » 8年前

Windows10,VS2015ではC#でみるとデフォルトでGACにあるみたいです。using System.Device.Location;が使えます。System.Device名前空間でクラス名ではありません。オブジェクトブラウザーで見るとこのようになります。http://csi.nisinippon.com/dev.png

C6b14

Re: C# System.Deviceがない

#4

投稿記事 by C6b14 » 8年前

using System.Windows.Threading;の追加が必要です。

C6b14

Re: C# System.Deviceがない

#5

投稿記事 by C6b14 » 8年前

[WPF で使ってないから]アセンブリWindowsBaseへの参照設定も必要です。

dic
記事: 658
登録日時: 14年前
住所: 宮崎県
連絡を取る:

Re: C# System.Deviceがない

#6

投稿記事 by dic » 8年前

WindowsBase
using System.Windows.Threading;
を追加しました。

しかし、ラムダ式がまだ理解不足でコードを少し変えたところ、また、エラーがでます。

31行
静的なフィールド、メソッド、またはプロパティ
System.Windows.Theading.Dispacter.BeginInvoke
(System.Delegate, System.WIndows.Threading.DispacterPriority,
params object[]) で、オブジェクト参照が必要です

46行
System.Windows.Threading.DispatcherPriority は型です
が、変数のように使用されています

コード:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Device.Location;
using System.Threading;
using System.Windows.Threading;

namespace pokemon
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            GeoCoordinateWatcher wtc = new GeoCoordinateWatcher();
            wtc.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(wtc_PositionChanged);
            wtc.Start(); 
        }
        delegate Delegate MyDel(TextBox textBox1, GeoPositionChangedEventArgs<GeoCoordinate> e);

        void wtc_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            MyClass mc = new MyClass();
            MyDel md;
            md = mc.wtc_Func;
            Dispatcher.BeginInvoke(
                md(textBox1,e),
                System.Windows.Threading.DispatcherPriority.Normal,
                null );
        }
    }
}

class MyClass
{
    public Delegate wtc_Func(TextBox textBox1, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        textBox1.Text =
            e.Position.Location.Latitude.ToString() + ", " +
            e.Position.Location.Longitude.ToString();
        return DispatcherOperation;
    }
}

C6b14

Re: C# System.Deviceがない

#7

投稿記事 by C6b14 » 8年前

Dispatcher.BeginInvoke と DispatcherOperation; でエラー になるのですが 原因不明なので delegate の宣言と ラムダが一致してるかよく調べて見て下さい。

C6b14

Re: C# System.Deviceがない

#8

投稿記事 by C6b14 » 8年前

ラムダはどうでしたか。エラーになるまえには表示はできてましたか。

C6b14

Re: C# System.Deviceがない

#9

投稿記事 by C6b14 » 8年前

ラムダだけのサンプルです。表示はします。

コード:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Device.Location;
using System.Windows.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            GeoCoordinateWatcher wtc = new GeoCoordinateWatcher();
            wtc.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(wtc_PositionChanged);
            wtc.Start();
        }
        //delegate System.Delegate MyDel(TextBox textBox1, GeoPositionChangedEventArgs<GeoCoordinate> e);
        delegate int MyDel(TextBox textBox1, GeoPositionChangedEventArgs<GeoCoordinate> e);//delegate

        void wtc_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            MyClass mc = new MyClass();//mcインスタンス作成
            MyDel md;//ラムダ
            md = mc.wtc_Func;//ラムダ=mc.wtc_Func

            int ret = md(textBox1, e);//mdラムダ=mc.wtc_Func
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
      
    }
    class MyClass//ラムダ=mc.wtc_Func用クラス
    {            //ラムダ=mc.wtc_Func用関数
        public int wtc_Func(TextBox textBox1, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            textBox1.Text =
                e.Position.Location.Latitude.ToString() + ", " +
                e.Position.Location.Longitude.ToString();
            
            return 123;
        }
    }
}

YuO
記事: 947
登録日時: 14年前
住所: 東京都世田谷区

Re: C# System.Deviceがない

#10

投稿記事 by YuO » 8年前

ざっと見たところだと,
  • WinFormsのGUIスレッドで実行を行いたいのだから,DispatcherではなくControlのInvokeまたはBeginInvokeを使う。
    今回はFormの内部だからthis.Invokeまたは単にInvokeでよい。
  • Delegateはそれだけで1つのオブジェクト。引数には,そのまま渡さないと意味がない。
    md(textBox1, e)のようにしたら,メソッドを呼び出してしまうし,System.Delegateを返さないと辻褄が合わなくなってしまう。
  • 使った事の無い.NET Frameworkのクラス・メンバーを使うのであれば,まずはMSDNを参照。
    Dispatcher.BeginInvoke メソッド (Delegate, DispatcherPriority, Object[])とかControl.BeginInvoke メソッド (Delegate, Object[])とか。
  • 固有のDelegateを定義する必要があるのは,だいたいout/refが絡むときのみ。それ以外はSystem.ActionやSystem.Funcで間に合う。
    今回であれば,Action<TextBox, GeoPositionChangedEventArgs<GeoCoordinate>>。
あたりでしょうか。

C6b14

Re: C# System.Deviceがない

#11

投稿記事 by C6b14 » 8年前

まずDispatcher.BeginInvokeのDispatcher.は不要です。戻り値のDispatcherOperationはSystem.Delegate型でないので間違っています。ラムダ式の基本を理解する必要があります。(決まり事を知らないと理解できない。慣れると簡単です。)サンプルを動作させて動きを見てください。

コード:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Device.Location;
using System.Windows.Threading;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            GeoCoordinateWatcher wtc = new GeoCoordinateWatcher();
            wtc.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(wtc_PositionChanged);
            wtc.Start();
        }
        //delegate System.Delegate MyDel(TextBox textBox1, GeoPositionChangedEventArgs<GeoCoordinate> e);
        delegate int MyDel(TextBox textBox1, GeoPositionChangedEventArgs<GeoCoordinate> e);//delegate
        delegate int del(int i); //delegate②デリゲート:普通は暗黙的に定義されてい目につかない
        //◆下記の様に沢山の定義済みデリゲートがあるので

        void wtc_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            MyClass mc = new MyClass();//mcインスタンス作成
            MyDel md;//ラムダ
            md = mc.wtc_Func;//ラムダ=mc.wtc_Func
            int ret = md(textBox1, e);//mdラムダ=mc.wtc_Func

            del d = (i) => i * i;//ラムダ②:一般的にはこの形をラムダ(ラムダ式)と言う:[匿名関数という]

            this.Text = d(9).ToString();//ラムダ②の使用例:d(9) => 9*9=81:数学のラムダ式に似ている
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
    class MyClass//ラムダ=mc.wtc_Func用クラス
    {            //ラムダ=mc.wtc_Func用関数
        public int wtc_Func(TextBox textBox1, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            textBox1.Text =
                e.Position.Location.Latitude.ToString() + ", " +
                e.Position.Location.Longitude.ToString();
            
            return 123;
        }
    }
}
/*
 *  ◆ 定義済みデリゲート
◾戻り値のない定義済みデリゲートのAction
◾戻り値を持つ定義済みデリゲートのFunc
◾論理型のも戻り値を持つ定義済みデリゲートのPredicate
具体的定義
public delegate void Action()
public delegate void Action<in T>(T arg)
public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2)
public delegate void Action<in T1, in T2, in T3>(T1 arg1, T2 arg2, T3 arg3)
public delegate void Action<in T1, in T2, in T3, in T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
public delegate TResult Func<out TResult>()
public delegate TResult Func<in T, out TResult>(T arg)
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2)
public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3)
public delegate TResult Action<in T1, in T2, in T3, in T4, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4)
public delegate bool Predicate<T>(T arg)
というような感じで定義されています。
.NET Framework 4.5ではActionやFuncに指定される引数の数が最大16まであるデリゲートが定義されています。
*/

C6b14

Re: C# System.Deviceがない

#12

投稿記事 by C6b14 » 8年前

後は、他の方の言われてるようにすればいいと思います。(ラムダ式を引数として書くときのことにについて言われていることに注意してください。)

C614b

Re: C# System.Deviceがない

#13

投稿記事 by C614b » 8年前

(コールバック関数を登録するときは”関数名”(=ラムダ)だけでいいので引数を書くと怪しい動作になります。)

dic
記事: 658
登録日時: 14年前
住所: 宮崎県
連絡を取る:

Re: C# System.Deviceがない

#14

投稿記事 by dic » 8年前

デリゲートとラムダ式の組み合わせってところでつまづいているようでした。

>>デリゲートそのもので、オブジェクト
これを理解していなかったようです。

https://code.msdn.microsoft.com/Windows ... o-2cb262e9
を参考にしたのですが、Dispatcherがなにか分からなかったのも
原因かなと思ってます。

C614b 様のサンプルで確かに動作しました。
しかし、私のPCにはGPSがついてないので、仮想ドライバーをインストールする画面に移行
になり、そこまででした。


C614b 様の回答されたサンプルで動いたので良しとします。
C614b 様、YuO様 ありがとうございました。

C6b14

Re: C# System.Deviceがない

#15

投稿記事 by C6b14 » 8年前

Silverlightアプリケーションの例ならWPFアプリケーションの方が楽だったかも知れませんね。

C6b14

Re: C# System.Deviceがない

#16

投稿記事 by C6b14 » 8年前

[追記]>今回であれば,Action<TextBox, GeoPositionChangedEventArgs<GeoCoordinate>>。
についてはここhttp://qiita.com/RyotaMurohoshi/items/7 ... 2889cf07deを参考にすればいいと思います。

閉鎖

“C言語何でも質問掲示板” へ戻る