我正在使用.NET使用静态地图从Google地图加载地图图块.
我遇到的问题是我不知道返回图像的SW和NE坐标是什么.
我发现了许多不同的代码示例,公式,但它们似乎都有缺陷.这是最接近正确答案的那个.当我在Google地图中输入坐标时,它显示它有点偏离.
var result = GoogleMapsAPI.GetBounds(new Coordinate(4.79635, 51.15479), 20, 512, 512);
public static class GoogleMapsAPI
{
public static MapCoordinates GetBounds(Coordinate center, int zoom, int mapWidth, int mapHeight)
{
var scale = Math.Pow(2, zoom);
var SWPoint = new Coordinate(center.X - (mapWidth / 2) / scale, center.Y - (mapHeight / 2) / scale);
var NEPoint = new Coordinate(center.X + (mapWidth / 2) / scale, center.Y + (mapHeight / 2) / scale);
return new MapCoordinates() { SouthWest = SWPoint, NorthEast = NEPoint …Run Code Online (Sandbox Code Playgroud) 我一直在尝试并尝试使我的通用扩展方法工作,但他们只是拒绝,我无法弄清楚为什么. 虽然它应该,但这个帖子对我没有帮助.
当然我已经查到了如何,在我看到他们说它很简单的时候应该是这样的语法:(
在某些地方我读到我需要在参数decleration之后添加"where T:[type]",但是我的VS2010只是说这是一个语法错误.)
using System.Collections.Generic;
using System.ComponentModel;
public static class TExtensions
{
public static List<T> ToList(this IEnumerable<T> collection)
{
return new List<T>(collection);
}
public static BindingList<T> ToBindingList(this IEnumerable<T> collection)
{
return new BindingList<T>(collection.ToList());
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我得到这个错误:
找不到类型或命名空间名称"T"(您是否缺少using指令或程序集引用?)
如果我然后更换
public static class TExtensions
Run Code Online (Sandbox Code Playgroud)
通过
public static class TExtensions<T>
Run Code Online (Sandbox Code Playgroud)
它给出了这个错误:
必须在非泛型静态类中定义扩展方法
任何帮助将不胜感激,我真的被困在这里.
我一直在寻找,但我似乎无法找到我正在寻找的东西,所以我会在这里试一试.
情况:我有MainWindow和MainWindowData类.在MainWindowData中,只有使用属性UpdateGUI定义的公共属性.
public class UpdateGUI : Attribute { }
public class MainWindowData
{
[UpdateGUI]
public string TESTVAR { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我想在MainWindowData中为每个属性的setter方法添加一个方法.更加具体:
void OnPropertyChanged(String PropertyName);
Run Code Online (Sandbox Code Playgroud)
我想我会在MainWindow构造函数中获取所有UpdateGUI属性,然后以某种方式添加另一个方法,但这就是我被困住的地方.我使用此代码来获取所有属性,其工作原理:
List<PropertyInfo> properties = (from pi in typeof(MainWindowData).GetProperties()
where pi.GetCustomAttributes(typeof(UpdateGUI), false).Any()
select pi).ToList();
Run Code Online (Sandbox Code Playgroud)
这给了我一个很好的列表,列出了我必须更新的所有属性.
所以问题是:我怎样才能使属性动态地从以下变换:
[UpdateGUI]
public string TESTVAR { get; set; }
Run Code Online (Sandbox Code Playgroud)
至:
[UpdateGUI]
private string _TESTVAR;
public string TESTVAR {
get {
return _TESTVAR;
}
set {
_TESTVAR = value;
OnPropertyChanged("TESTVAR");
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助!非常感谢:)
问候
我有一个自定义类,它依赖于传递类型T来传递.我只知道字符串形式是什么类型,因为它是如何被发送的.我一直在寻找,但似乎无法找到我需要的确切内容.我可以将字符串值解析为类型,但我需要将其解析为...某些东西,我可以作为泛型参数传递.
我已经改写了我的问题,因此:
// Classes structure
namespace Mynamespace
{
public interface IRequest
{
}
public interface IHandler<T> where T : IRequest
{
void Handle(T item);
}
public class MyRequest : IRequest
{
}
public class MyHandler : IHandler<MyRequest>
{
void Handle(MyRequest item)
{
}
}
}
// The info I get, and I know typeString is a IRequest
string typeString = "My";
object requestItem = [insert xml parsing here];
// I then create a handler, to handle the request
Type typeHandler …Run Code Online (Sandbox Code Playgroud) 我想要的代码:
<Extension()>
Public Function NValue(Of T)(ByVal value As Nullable(Of T), ByVal DefaultValue As T) As T
Return If(value.HasValue, value.Value, DefaultValue)
End Function
Run Code Online (Sandbox Code Playgroud)
基本上我想要它做的是将默认值赋给可空对象,并且根据它是否为null,它将给出自己的值或默认值.
所以使用Date对象,它会这样做,这有效,但我无法使用通用T:
<Extension()>
Public Function NValue(ByVal value As Date?, ByVal DefaultValue As Date) As Date
Return If(value.HasValue, value.Value, DefaultValue)
End Function
Dim test As Date? = Nothing
Dim result = test.NValue(DateTime.Now)
Run Code Online (Sandbox Code Playgroud)
变量'result'现在具有当前的DateTime.
当我用T尝试它时,我得到它作为错误(Visual Studio在Nullable(Of T)中放置T:类型'T'必须是值类型或约束为'Structure'的类型参数才能使用使用'Nullable'或nullable modifier'?'.
非常感谢您的帮助!
问候