我试图从我的MainWindow.xaml.cs调用以下方法,但我不确定如何操作。
public static string TileString(int x, int y, int z)
{
int[] tileInts = { x, y, z };
string tileString = string.Join("/", tileInts);
return tileString;
}
该方法驻留在具有从数据库获取图像的某些功能的类中,并且我想要使用这些图像的名称/ id更新我在MainWindow中的文本框。该功能正常工作,并在" tileString"中返回。但是,我不确定如何将它与我的MainWindow结合在一起。任何提示?在最佳世界中,我只能说(在MainWindow.xaml.cs中)ServerOutPutTextBox.Appendtext(tileString); (我希望它显示在文本框中),但它无法找到它。
我想象以下内容:
public static string TileString(int x, int y, int z)
{
int[] tileInts = { x, y, z };
string tileString = string.Join("/", tileInts);
ServerOutPutTextBox.Appendtext(tilestring);
}
或以某种方式在我的MainWindow.xaml.cs中调用TileString。
编辑:全班同学。public class Response
{
public static string tileString;
public static List<string> DbLocationList;
public static HttpListener listener;
public static void StartListening(List<string> prefixes, List<string> dbLocation)
{
if( listener == null )
listener = new HttpListener();
//if (prefixes == null || prefixes.Count == 0)
// throw new ArgumentException("Prefixes missing.");
DbLocationList = dbLocation;
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
try
{
listener.Start();
listener.BeginGetContext(new AsyncCallback(OnRequest), listener);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
public static void OnRequest(IAsyncResult result)
{
//HttpListener listener = (HttpListener) result.AsyncState;
if (listener == null)
return;
HttpListenerContext context = listener.EndGetContext(result);
listener.BeginGetContext(new AsyncCallback(OnRequest), listener);
string url = context.Request.RawUrl;
string[] split = url.Split('/');
int lastIndex = split.Length - 1;
HttpListenerResponse response = context.Response;
int x, y, z;
string prefix;
string dbPath = "";
try
{
prefix = split[lastIndex - 3];
x = Convert.ToInt32(split[lastIndex - 1]);
y = Convert.ToInt32(split[lastIndex]);
z = Convert.ToInt32(split[lastIndex - 2]);
foreach (var path in DbLocationList)
{
string[] tmpStrings = path.Split('\\');
string dbFn = tmpStrings[tmpStrings.Length-1];
if (prefix == dbFn)
{
dbPath = path;
break;
}
}
// To extract from the database
TilePicker picker = new TilePicker(dbPath);
Image img = picker.GetTile(x, y, z);
TileString(x, y, z);
if (img == null)
{
response.StatusCode = 404;
response.OutputStream.Close();
}
else
{
// Load the image
//Bitmap bm = new Bitmap(path);
MemoryStream bmStream = new MemoryStream();
//bm.Save(bmStream, ImageFormat.Png);
img.Save(bmStream, ImageFormat.Png);
byte[] buffer = bmStream.ToArray();
// Get a response stream and write the response to it.
response.ContentLength64 = bmStream.Length;
response.ContentType = "image/png";
response.KeepAlive = true;
Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
// You must close the output stream.
output.Close();
}
//listener.Stop();
//}
response.Close();
}
catch (Exception)
{
response.StatusCode = 404;
response.OutputStream.Close();
}
}
public static string TileString(int x, int y, int z)
{
int[] tileInts = { x, y, z };
string tileString = string.Join("/", tileInts);
return tileString;
}
}
答案 0 :(得分:1)
解决此问题的一种方法是,向MainWindow添加一个公共方法,并从TileString访问该方法:
// add this method to MainWindow
public void AppendServerOutput(string output)
{
ServerOutPutTextBox.Appendtext(output);
}
你可以像以下一样使用它:
public static string TileString(int x, int y, int z)
{
...
Application.Current.Dispatcher.Invoke(new Action(() =>
{
var mainWindow = Application.Current.MainWindow as MainWindow;
mainWindow.AppendServerOutput(tileString);
}));
}