Использование проблемы SocketConnection с Blackberry с помощью MDS

попробуйте что-то вроде этого.....

<Grid DataContext="{Binding Path=HPVM}">
</Grid>

, где HPVM является общедоступным членом этого->, Ваша форма и т.д.

Создает экземпляр Вашего класса в xaml, путем добавления чего-то вроде этого к разделу ресурсов.... (не забывайте добавлять xmlns пространство имен)

<my:bogart x:Key="franken"/>

тогда, ограничьте контекст данных к статическому ресурсу, который Вы просто добавили....

<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource franken}">
    <TextBox  Background="Red" Foreground="White" Text="{Binding Path=sum}"  />
</Grid>
5
задан Maksym Gontar 18 September 2009 в 13:15
поделиться

2 ответа

As I indicated in a comment above, I needed a way to determine if a device I am connecting to is not there, and I do that by seeing if our 'ping' returns any data. If the device is not there it will block. I cannot rely on that behavior. Another issue that crept up while solving this is that the read(...) methods of the RIM InputStream class block if you provide a buffer bigger than the data you want back. But how am I supposed to know how much data is there if available() returns 0? Reading byte-by-byte is about the only way to do this, but it still blocks if there is no data.

To address this I followed the theme of the 1st answer, but I put this method on its own thread and had it write to a separate byte buffer. I created a class that extended InputStream and implemented available() and read(...). Available returns how many bytes are in the byte buffer, and read only gives back however much is in the buffer or however much the caller requests, whichever is less.

This setup lets me use an InputStream interface, but behind the scenes it is just a continuously running reader thread that is alive until the connection is dropped. At that time the read, if blocked, will throw an exception to indicate the connection closed. This behavior is fine as it can be easily handled.

Thanks to all above who helped with this issue. Your thoughts help move towards the solution.

0
ответ дан 15 December 2019 в 06:31
поделиться

Общий контракт метода InputStream.available () заключается в том, что он «возвращает количество байтов, которые могут быть прочитаны (или пропущены) из этого входного потока без блокировки следующим вызывающим элементом метод для этого входного потока ". Следовательно, в большинстве реализаций нет гарантии, что он вернет длину содержимого читаемого потока. Следовательно, лучше читать его следующим образом

byte[] readFromStream(InputStream is) throws IOException
{
    byte[] data = new byte[4096];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    int count = is.read(data, 0, data.length);
    while (count != -1)
    {
        dos.write(data, 0, count);
        count = is.read(data, 0, data.length);
    }

    data = baos.toByteArray();

    return data;
}

Вы вызываете метод readFromStream () и получаете возвращенный byte [].

3
ответ дан 15 December 2019 в 06:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: