每30秒从CSV中选取一行并将其绑定到用户控件 [英] Pick up a line from a CSV each 30 seconds and binding it to a user control
问题描述
我是C#和WPF的初学者。
我想从CSV中读取。我想每隔30秒从CSV中选取一行,并使用行的字段更新用户控件中的值(带有文本框的仪表)。
我已经拥有的内容:我正在做一个与按钮事件类似的事情。我正在使用 StreamReader 和 ReadLine 读取CSV,我将数据存储在List< string>中。我用于用户控件 INotifypropertyChanged ,我将控件属性绑定到数据上下文(列表中的值)。然后,当我按下按钮时,值将从下一个csv行(下一个列表位置)获取,然后更新到用户控件。
我想自动完成它,我想每30秒读一下下一行。我读了一下定时器,我做了一些测试打印列表的每个5秒的对象,但它不适用于我的计划。 对我的目标有任何建议吗? 我对活动和时段没有多少经验。
我的在这种情况下代码是不相关的,因为我询问如何每隔30秒触发,读取,拾取一行或执行数据更新,以防万一我添加了主代码块:
用户控制:
I am a beginner with C# and WPF.
I would like to read from a CSV . I would like to pick up a line from the CSV each 30 second and with the fields of the line update the values in a User control ( a gauge with a Text box).
What I have already: I am doing something similar with a button event. I am reading the CSV with StreamReader and ReadLine and I am storing the data in a List<string>. I use for the user control INotifypropertyChanged and I am binding the control properties to a data context ( values from the list). Then, when I press the button , the values are taken from to the next csv line (next list position) and consequently are updated into the user control.
I want to do it automatically , I want to read the next line each 30 seconds.I read about timer and I did some tests printing the objtects of the list each 5 secodnds ,but it is not working for my plan. Any recommendation for my objective? I don't have to much experience with events and periods.
My code in this case it is not relevant because I am asking about how to fire , read , pick up a line or execute the data update each 30 second but just in case I added the main code blocks:
User control:
<pre lang="c#"> <Viewbox Grid.Column="1" >
<gauge:CircularGaugeControl x:Name="gaugeWindAngle" Grid.Column="1" Grid.Row="0" Margin="5"
Radius="105"
ScaleRadius="60"
ScaleStartAngle="140"
CurrentValue="{Binding Type}"
RangeIndicatorThickness="9"
RangeIndicatorRadius="45"
RangeIndicatorLightRadius="8"
PointerThickness ="5"
DialTextOffset="40"
DialText="Wind Angle"
DialTextColor="SlateGray"
/>
</Viewbox >
<WrapPanel Margin="5">
<Label Foreground="#DFA297">Speed: </Label>
<TextBox
x:Name="textboxWindSpeed" Text="{Binding TalkerID , UpdateSourceTrigger=PropertyChanged}"/>
</WrapPanel>
<Button Grid.Column="0" Content="Send sentence" x:Name="b1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Click="Button_Click" />
</Grid>
代码背后
CODE BEHIND
public partial class Wind_gauge : UserControl, INotifyPropertyChanged
{
Trucker trucker = new Trucker();//DECLARE DATA CONTEXT AND LINK IT TO THE WIDGET properties (VALUES)
double windAngle;
int click = 0;
public double WindAnglevalue
{
get { return windAngle; }
set
{
windAngle = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("WindAnglevalue"));
}
}
}
double windSpeed;
public double WindSpeedvalue
{
get { return windSpeed; }
set
{
windSpeed = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("WindSpeedvalue"));
} }
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public Wind_gauge()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);//launched event
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
this.gaugeWindAngle.DataContext = trucker.sentenceReceived; // Initialize widjet value to data context
this.textboxWindSpeed.DataContext = trucker.sentenceReceived;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
sendsentence(sender, e);
click++;//List index
}
在类Truker中读取我的CSV列表< string> sentenceListt;:
READING MY CSV IN class Truker to List<string>sentencesListt;:
class Trucker
{ static Timer _timer;//test
public List<string> s = new List<string>(); //Repository of NMEA messages recived
public List<string> sentencesList
{
get {
if (s == null) {
loadNMEAsentences();
}
return s;
} }
public Trucker()
{
loadNMEAsentences();// Load messages from CSV
/* foreach (string l in sentencesList)
{
Console.Write("Sentences:" + l);
}
Console.Read();*/
}
public void loadNMEAsentences()
{
_timer = new Timer(3000);
_timer.Enabled = true;
_timer.Elapsed += new ElapsedEventHandler(timerRead);
}//end method
void timerRead(object sender, ElapsedEventArgs e)
{ s= new List<string>();
System.IO.StreamReader file =
new System.IO.StreamReader(@"C:\Users\........\Data\NMEAmsg_wind.csv");
string line;
while ((line = file.ReadLine()) != null)
{ if (line.IndexOf("$IIMWV", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
s.Add(line);// Insert sententece to the repository sentences list
// Console.WriteLine(line);
}
}
file.Close(); }
推荐答案
IIMWV\", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
s.Add(line);// Insert sententece to the repository sentences list
// Console.WriteLine(line);
}
}
file.Close(); }
IIMWV", StringComparison.CurrentCultureIgnoreCase) >= 0) { s.Add(line);// Insert sententece to the repository sentences list // Console.WriteLine(line); } } file.Close(); }
I solve it , I apply the timer into the user control and elapse the interval with a read event:
I solve it , I apply the timer into the user control and elapse the interval with a read event:
public Wind_gauge()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);//launched event
timer = new Timer(3000);
timer.Enabled = true;
timer.Elapsed += new ElapsedEventHandler(timerRead);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
// this.gauge_rpm.DataContext = TRC.Fields.RPMDemandValue.ToString(); //Remaind it
this.gaugeWindAngle.DataContext = trucker.sentenceReceived; // Initialize widjet value to data context
this.textboxWindSpeed.DataContext = trucker.sentenceReceived; // Initialize widjet value to data context
}
void timerRead(object sender, ElapsedEventArgs e)
{
//pick up a sentence here . It would be executed each 3 seconds
if (click < trucker.sentencesList.Count)
{
string[] fields = trucker.sentencesList[click].ToString().Split(',');
trucker.sentenceReceived.Type = fields[1].ToString();
trucker.sentenceReceived.TalkerID = fields[1].ToString();
}
click++;//Index list
}
The reading Csv step and store the data into the list is as I did in the code of the cuestion without timer.
The reading Csv step and store the data into the list is as I did in the code of the cuestion without timer.
这篇关于每30秒从CSV中选取一行并将其绑定到用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!