How to send parameters from class to winform?
I've found a code that does exactly what I like: View/edit ID3 data for MP3 files
...
class a{
public ? getContent(){
开发者_开发问答
string Title = Encoding.Default.GetString(tag.Title);
string Artist = Encoding.Default.GetString(tag.Artist);
string Album = Encoding.Default.GetString(tag.Album);
}
}
class form1{
button1.click()
{
? = a.getContent
text1.text = ?.Title;
text2.Text = ?.Artist;
}
}
Simucal print the result to the console but I'd like to have a winform that gets this input and puts it in some textboxes. I guess I can do it with arrays but I guess there are better ways to do it in the mvvm way (I know that my question may make no sense but I like to do it the right way)...
Please help :)
Is there some reason you can't use a class to encapsulate that data?
class TagData
{
public string Title {get; set; }
public string Artist {get; set; }
public string Album {get; set; }
}
class a{
public TagData getContent(){
return new TagData
{
Title = Encoding.Default.GetString(tag.Title),
Artist = Encoding.Default.GetString(tag.Artist),
Album = Encoding.Default.GetString(tag.Album)
};
}
}
class form1{
button1.click()
{
var tagData = a.getContent
text1.text = tagData.Title;
text2.Text = tagData.Artist;
}
}
Alternatively, if you want to be less 'safe' about it, you could just pack it all into a Dictionary:
class a{
public Dictionary<string, string> getContent(){
var tagData = new Dictionary<string, string>();
tagData["Title"] = Encoding.Default.GetString(tag.Title);
tagData["Artist"] = Encoding.Default.GetString(tag.Artist);
tagData["Album"] = Encoding.Default.GetString(tag.Album);
return tagData;
}
}
class form1{
button1.click()
{
var tagData = a.getContent();
text1.Text = tagData["Title"];
text2.Text = tagData["Artist"];
}
}
In this situation, you would simply return a result object containing that data:
public ContentData getContent()
{
return new ContentData
{
Title = "Hello World",
Artist = "Some Other String"
};
}
ContentData data = someobject.getContent();
text1.Text = data.Title;
// etc
You would just make your own type.
Create a class called Tags
or something similar with the properties you want. Return an instance of that with the properties set. Winforms doesn't do MVVM very well.
精彩评论