C# System.Deviceがない
C# System.Deviceがない
[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 を使用しないといけない(?)
[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 を使用しないといけない(?)
Re: C# System.Deviceがない
すいません、ある程度わかりました。
ここまでできたのですが、エラーが以下の通りにでます
名前 Dispatcher は現在のコンテキスト内にありません。
using System.Threading; しているのですが、このエラーがなくなりません。
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; しているのですが、このエラーがなくなりません。
Re: C# System.Deviceがない
Windows10,VS2015ではC#でみるとデフォルトでGACにあるみたいです。using System.Device.Location;が使えます。System.Device名前空間でクラス名ではありません。オブジェクトブラウザーで見るとこのようになります。http://csi.nisinippon.com/dev.png
Re: C# System.Deviceがない
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.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;
}
}
Re: C# System.Deviceがない
Dispatcher.BeginInvoke と DispatcherOperation; でエラー になるのですが 原因不明なので delegate の宣言と ラムダが一致してるかよく調べて見て下さい。
Re: C# System.Deviceがない
ラムダだけのサンプルです。表示はします。
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;
}
}
}
Re: C# System.Deviceがない
ざっと見たところだと,
- 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>>。
Re: C# System.Deviceがない
まず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まであるデリゲートが定義されています。
*/
Re: C# System.Deviceがない
後は、他の方の言われてるようにすればいいと思います。(ラムダ式を引数として書くときのことにについて言われていることに注意してください。)
Re: C# System.Deviceがない
デリゲートとラムダ式の組み合わせってところでつまづいているようでした。
>>デリゲートそのもので、オブジェクト
これを理解していなかったようです。
https://code.msdn.microsoft.com/Windows ... o-2cb262e9
を参考にしたのですが、Dispatcherがなにか分からなかったのも
原因かなと思ってます。
C614b 様のサンプルで確かに動作しました。
しかし、私のPCにはGPSがついてないので、仮想ドライバーをインストールする画面に移行
になり、そこまででした。
C614b 様の回答されたサンプルで動いたので良しとします。
C614b 様、YuO様 ありがとうございました。
>>デリゲートそのもので、オブジェクト
これを理解していなかったようです。
https://code.msdn.microsoft.com/Windows ... o-2cb262e9
を参考にしたのですが、Dispatcherがなにか分からなかったのも
原因かなと思ってます。
C614b 様のサンプルで確かに動作しました。
しかし、私のPCにはGPSがついてないので、仮想ドライバーをインストールする画面に移行
になり、そこまででした。
C614b 様の回答されたサンプルで動いたので良しとします。
C614b 様、YuO様 ありがとうございました。
Re: C# System.Deviceがない
[追記]>今回であれば,Action<TextBox, GeoPositionChangedEventArgs<GeoCoordinate>>。
についてはここhttp://qiita.com/RyotaMurohoshi/items/7 ... 2889cf07deを参考にすればいいと思います。
についてはここhttp://qiita.com/RyotaMurohoshi/items/7 ... 2889cf07deを参考にすればいいと思います。